好的,在我修复this issue后,现在我有了这个新的,我似乎无法理解。
这是我的观点和Javascript代码:
<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 %>
我的控制器
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
当我点击“刷新”按钮时,收到以下错误消息:
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)
我尝试了很多方法通过Javascript将“:project”参数传递给控制器,但我没有运气。
感谢任何帮助?
谢谢&amp;最诚挚的问候
更新1 根据要求,这些是我为get_config和set_config方法添加的路由
resources :projects do
collection do
get :get_config
get :set_config
end
end
工作解决方案1
这是对我有用的代码更改:我在表单外移动了按钮并将其替换为以下
<%= button_to 'Get Config', get_config_projects_url(id: @project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>
工作解决方案2(解决方法)
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(),
}
});
}
答案 0 :(得分:1)
对于那些多年后观看的人。 我认为您需要在您的数据下放置一个“项目”对象参数,以包含您想要的参数。对于其他任何人来说,它是对象名称(:page,:person etc)
Rails期望在其控制器中实现这一点。
如此;
data: {
project: {
id: <%= @attero.id %>,
unit_mgt_address: $('.unit_mgt_address_class').val()
}
}
答案 1 :(得分:0)
这会有用吗?
data: {
"id": <%= @project.id %>,
"project[UnitMgtAddress]": $('.unit_mgt_address_class').val(),
}
答案 2 :(得分:0)
你应该让Rails为你处理你的AJAX请求。我在提交的表单之外拉了那个按钮并将其放入自己的link_to
或button_to
,如果你想使用一个method: :get
,只需添加remote: true
按钮。设置<%= link_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true %>
,它将为您处理所有AJAX工作。
<%= button_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>
或
button_to
语法可能需要稍微调整一下,特别是对于你的路径,但是让Rails管理这类事情要比你尝试做的更清晰。阅读{{1}} here的语法。