本文介绍了Ajax请求引起"参数丢失或为空值"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我固定在this问题,现在我有这个新的,我似乎并没有围绕我的头。

这是我的看法和Javascript code:

 < SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>

传播的getconfig(){
$阿贾克斯({
    网址:<%= get_config_projects_url%>中,
    数据: {
        编号:LT;%= @ p​​roject.id%>中
        unit_mgt_address:$('。unit_mgt_address_class)VAL()。
    }
});
}

< / SCRIPT>

<%=的form_for(@project)办| F | %>
  <输入类型=按钮的onClick =的getconfig()/>
  < D​​IV CLASS =字段>
    <%= f.label:单位管理地址%>< BR>
    <%= f.text_field:UnitMgtAddress,:类=>'unit_mgt_address_class%>
  < / DIV>
  < D​​IV CLASS =行动>
    &其中;%= f.submit%GT;
  < / DIV>
<%结束%GT;
 

我控制器

 类ProjectsController<的ApplicationController
  before_action:set_project,只有:[:显示,编辑,:更新:灭]

  <许多其他的方法去这里>

  #GET /项目/ 1
  #GET /projects/1.json
  高清get_config

    @project = Project.find(PARAMS [:ID])

    respond_to代码做|格式|
      如果@ project.update(project_params)
        format.js
        的format.html {渲染操作:编辑,通知:!项目Get_Config成功 }
      其他
        的format.html {渲染的动作:更新}
        format.json {渲染JSON:@ project.errors,状态:unprocessable_entity}
      结束
    结束
  结束

  私人

  #使用回调分享共同的设置或动作之间的约束。
  高清set_project
    @project = Project.find(PARAMS [:ID])
  结束

  #从不信任的参数从可怕的网络,只允许白名单通过。
  高清project_params
    params.require(:项目).permit(:UnitMgtAddress)
  结束
结束
 

当我点击刷新按钮,出现此错误消息:

 入门GET/projects/get_config?id=9&unit_mgt_address=5.5.5.5为127.0.0.1,在2014年4月29日22时48分37秒-0700
处理由ProjectsController#get_config作为* / *
  参数:{身份证=>中9,unit_mgt_address=>中5.5.5.5}
  项目负荷(1.0ms的)选择项目。*从项目WHERE项目,ID=? LIMIT 1 [身份证,9]
已完成400错误的请求在6ms的

ActionController的:: ParameterMissing(参数丢失或为空值:项目):
  应用程序/控制器/ projects_controller.rb:128:在'project_params
  应用程序/控制器/ projects_controller.rb:91:在`块get_config
  应用程序/控制器/ projects_controller.rb:89:在'get_config


  渲染C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  渲染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的)
  渲染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的)
  渲染C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (63.0ms)
 

我试了很多方法来传递:项目参数,通过Javascript控制器,但我没有运气

任何帮助是AP preciated?

感谢和放大器;顺祝商祺

更新1 按照要求,这些是我添加了get_config和set_config方法

路线

 资源:项目办
  集合做
    得到:get_config
    得到:set_config
  结束
结束
 

工作方案1

这是为我工作的code修改:我搬到了按钮的形式外,用它取代了以下

 <%= button_to'获取配置,get_config_projects_url(ID:@ project.id,项目:{unit_mgt_address:@ project.UnitMgtAddress}),遥控器:真,方法:拿到%GT;
 

工作方案2(解决方法)

 函数的getconfig(){
    $阿贾克斯({
        网址:<%= get_config_projects_url(项目:{ID:@ project.id,unit_mgt_address:@ project.UnitMgtAddress})%>中,
        数据: {
            编号:LT;%= @ attero.id%>中
            unit_mgt_address:$('。unit_mgt_address_class)VAL()。
        }

    });
}
 

解决方案

您应该真正让Rails的处理你的AJAX请求你。我拉了正在被提交表单以外的按钮,并把它变成自己的的link_to button_to ,只是添加方法:获得如果你想使用一个按钮。设置远程:真正的,它会处理所有的AJAX为你工作

 <%=的link_to'获取配置,get_config_projects_url(@ project.id,项目:{unit_mgt_address:@ project.UnitMgtAddress}),遥控器:真%>
 

 <%= button_to'获取配置,get_config_projects_url(@ project.id,项目:{unit_mgt_address:@ project.UnitMgtAddress}),遥控器:真,方法:取% >
 

语法可能需要略微调整,对于特定的路径,而是让Rails的管理这样的事情是不是你想要做什么,更清洁了很多。阅读上的语法 button_to 的。

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 1As 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(),
        }

    });
}
解决方案

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.

这篇关于Ajax请求引起&QUOT;参数丢失或为空值&QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 02:45
查看更多