Ajax request causing “param is missing or the value is empty” error

我们两清 提交于 2019-12-22 10:44:38

问题


OK, after I fixed this issue, now I have this new one, which I don't seem to get my head around.

This is my view and Javascript code:

<script language="javascript" type="text/javascript">

function getConfig(){
$.ajax({
    url: "<%= get_config_projects_url %>",
    data: {
        id: <%= @project.id %>,
        unit_mgt_address: $('.unit_mgt_address_class').val(),
    }
});
}

</script>

<%= form_for(@project) do |f| %>
  <input type=button onClick="getConfig()"/>
  <div class="field">
    <%= f.label :Unit Management Address %><br>
    <%= f.text_field :UnitMgtAddress, :class=>'unit_mgt_address_class' %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

My Controller

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  <many other methods go here>

  # GET /projects/1
  # GET /projects/1.json
  def get_config

    @project = Project.find(params[:id])

    respond_to do |format|
      if @project.update(project_params)
        format.js
        format.html { render action: "edit", notice: 'Project Get_Config Successful!' }
      else
        format.html { render action: "update" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_project
    @project = Project.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def project_params
    params.require(:project).permit(:UnitMgtAddress)
  end
end

When I click on "Refresh" button, I get this error message:

Started GET "/projects/get_config?id=9&unit_mgt_address=5.5.5.5" for 127.0.0.1 at 2014-04-29 22:48:37 -0700
Processing by ProjectsController#get_config as */*
  Parameters: {"id"=>"9", "unit_mgt_address"=>"5.5.5.5"}
  Project Load (1.0ms)  SELECT  "projects".* FROM "projects"  WHERE "projects"."id"= ? LIMIT 1  [["id", 9]]
Completed 400 Bad Request in 6ms

ActionController::ParameterMissing (param is missing or the value is empty: project):
  app/controllers/projects_controller.rb:128:in `project_params'
  app/controllers/projects_controller.rb:91:in `block in get_config'
  app/controllers/projects_controller.rb:89:in `get_config'


  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (63.0ms)

I tried many ways to pass ":project" parameter to the controller via Javascript, but I had no luck.

Any help is appreciated?

Thanks & Best Regards

Update 1 As requested, these are the routes I added for get_config and set_config methods

resources :projects do
  collection do
    get :get_config
    get :set_config
  end
end

Working Solution 1

This is the code change that worked for me : I moved the button outside the form and replaced it with the following

<%= button_to 'Get Config', get_config_projects_url(id: @project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>

Working Solution 2 (workaround)

function getConfig(){
    $.ajax({
        url: "<%= get_config_projects_url(project: { id: @project.id, unit_mgt_address: @project.UnitMgtAddress }) %>",
        data: {
            id: <%= @attero.id %>,
            unit_mgt_address: $('.unit_mgt_address_class').val(),
        }

    });
}

回答1:


You should really let Rails handle your AJAX requests for you. I'd pull that button outside of the form that's being submitted and put it into its own link_to or button_to, just add method: :get if you want to use a button. Set remote: true and it will handle all the AJAX work for you.

<%= link_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true %>

or

<%= button_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>

Syntax might need to be tweaked slightly, for your path in particular, but letting Rails manage this sort of thing is a lot cleaner than what you're trying to do. Read up on the syntax for button_to here.




回答2:


For anyone viewing this many years later. I think you needed to put a "project" object param under your data to encompass your intended params. For anyone else it's the object name (:page, :person etc)

Rails expects this in its controllers.

so;

data: {
  project: {
    id: <%= @attero.id %>,
    unit_mgt_address: $('.unit_mgt_address_class').val()
  }    
}



回答3:


Will this work?

data: {
    "id": <%= @project.id %>,
    "project[UnitMgtAddress]": $('.unit_mgt_address_class').val(),
}


来源:https://stackoverflow.com/questions/23380844/ajax-request-causing-param-is-missing-or-the-value-is-empty-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!