[英]Ruby on Rails - call Controller Method and pass Parameters to it
I have following situation: 我有以下情况:
I have a risk model and on my view it shows me a table with all risks. 我有一个风险模型,在我看来,该模型向我显示了一张包含所有风险的表格。 The table also includes a
check_box_tag
, as each risk should be able to be checked. 该表还包含一个
check_box_tag
,因为应该可以检查每个风险。 In the tablehead there is a button and if this button is clicked, a method of the risk controller should be called, and it should have all checked risk IDs as parameter. 在表头中有一个按钮,如果单击此按钮,则应调用风险控制器的方法,并且该方法应将所有已检查的风险ID作为参数。
Actually, I don't know which would be the best way to solve this. 实际上,我不知道哪种方法是解决此问题的最佳方法。
I have following code so far: 到目前为止,我有以下代码:
View: 视图:
<table>
<thead>
<tr>
<th>Risk Name</th>
<th>
<button id="mergerisks">Merge Risks</button>
</th>
</tr>
</thead>
<tbody>
<% Risks.all.each do |risk| %>
<tr>
<td><%= risk.name %></td>
<td><%= check_box_tag 'mergerisks', risk.id %>
</td>
</tr>
<% end %>
</tbody>
</table>
Javascript: Javascript:
$( "#mergerisks" ).on( "click", function() {
var selectedriskmerge = new Array();
$("input:checked").each(
function() {
selectedriskmerge.push($(this).val());
});
});
Edit Added the following ajax call to javascript 编辑将以下ajax调用添加到javascript中
$.ajax({
url: '/riskmerge',
data:selectedriskmerge,
success:function() {
window.alert("Success!");
}
});
For now the Button only triggers the Javascript, and there are the ID's of all checked Risks are stored in an array. 现在,按钮仅触发Javascript,并且所有已检查风险的ID都存储在数组中。
But now I don't know what would be the best way to call a controller method in the risk controller and pass the IDs of all checked Risks to the method. 但是现在我不知道在风险控制器中调用控制器方法并将所有已检查风险的ID传递给方法的最佳方法是什么。
Thanks for any help. 谢谢你的帮助。
NOTE: This is slightly different from what you wanted, but might provide a better user experience since they won't have to click an additional button at the end to update the records so heed with caution. 注意:这与您想要的稍有不同,但是由于他们不必单击末尾的其他按钮来更新记录,因此可以提供更好的用户体验,因此请务必谨慎。 No matter what though you will get a decent example of how to use ajax for your own needs.
无论如何,您都会得到一个如何使用ajax满足自己需求的好例子。
First you need to create an action in the controller that updates the record you want which you already have. 首先,您需要在控制器中创建一个操作,以更新所需的记录。 The twist is that instead of rendering html you will want to render json at the end.
所不同的是,您将要在末尾呈现json,而不是呈现html。 Something like this will do.
这样的事情会做。
if risk.update_attribute(check: true)
render :json => { status: "Everything worked!"}
else
render :json => { status: "Something went wrong!"}
end
Next you will want to set up the javascript so that when a check box is clicked, an ajax post is sent to the action that updates the record. 接下来,您将需要设置javascript,以便在单击复选框时,将ajax帖子发送到更新记录的操作。 You have this partially done with your javascript.
您已使用JavaScript部分完成了此操作。 Inside your click event, you can have something like
在点击事件中,您可以看到类似
$.post("/risk_update",
// This line below is the parameters sent to the action.
//So your action will recognize params[:risk_id]
{ risk_id: $(".clicked_check_box").attr("value") },
"json")
.done(function (responseText) {
if (responseText.status === "Everything worked!") {
// Do something on success of info being saved
} else if (responseText.status === "Something went wrong!") {
// Do something on failure of info being saved
}
});
Finally, there is a problem with the check box. 最后,复选框有问题。 Do you want the user to uncheck the box and call a record again.
是否要用户取消选中该框并再次调用记录。 This of course goes beyond the discussion of the question but they are some things you have to keep in mind.
这当然超出了对问题的讨论范围,但是您必须牢记这些。
Of course in your case you will want to click a button that grabs all the ids and sends them to the action to update the records. 当然,在您的情况下,您将需要单击一个获取所有ID并将其发送到操作以更新记录的按钮。 One way would be to have javascript inject the id into an array when a check box is marked, then when the user clicks on the submit button the array is sent as params to the action that then loops through the arrays and updates the risks.
一种方法是让Javascript在选中复选框时将ID注入到数组中,然后当用户单击“提交”按钮时,将数组作为参数发送给操作,然后该操作遍历数组并更新风险。
I'm sure there are better ways though, that's just the first thing that came to mind. 我敢肯定,还有更好的方法,这只是我想到的第一件事。
Your best bet would be to place all of the check boxes inside of a form. 最好的选择是将所有复选框都放在表单内。
<%= form_tag controller_method_name_your_controller_path, :method => 'get', do %>
...
<%= submit_tag 'submit' %>
<% end %>
Declare the path in the routes.rb file under your_controller
在
your_controller
下的routes.rb文件中声明路径
get :controller_method_name
You can group you check boxes together by using check_box
: 您可以使用
check_box
复选框组合在一起:
<%= check_box 'mergerisks', risk.id %>
Then using params[:mergerisks]
in the controller will return a hash with risk.id
as the keys and a 1 or 0
depending on if the check box is checked. 然后在控制器中使用
params[:mergerisks]
将返回一个带有risk.id
作为键的哈希值,并根据是否选中此复选框返回1 or 0
。
Some documentation on routes and forms: 有关路线和表格的一些文档:
http://guides.rubyonrails.org/routing.html http://guides.rubyonrails.org/routing.html
http://guides.rubyonrails.org/form_helpers.html http://guides.rubyonrails.org/form_helpers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.