[英]Rails passing params from index page
I have a Rails app where I list records on an index page. 我有一个Rails应用,在其中我在索引页面上列出记录。 One of the columns is: 列之一是:
<td><%= check_box_tag "event_ids[]", event.id %></td>
I have a button on the bottom of the index page: 我在索引页面的底部有一个按钮:
<%= link_to 'Submit ', addinvtimes_invtimes_path, :class => 'btn btn-success' %>
I wanted this code to run in the invtimes controller: 我希望此代码在invtimes控制器中运行:
def addinvtimes
if params[:event_ids] != nil
params[:event_ids].each do |i|
newinvtime = Invtime.new(
linetype_id: 1,
invoice_id: @invoice.id,
event_id: i.to_i
)
end
end
end
But, params[:event_ids]
is nil even if several boxes are checked. 但是,即使选中了多个复选框, params[:event_ids]
也为零。
Can't you submit params using an index page? 您不能使用索引页提交参数吗? Does it have to be a form? 它一定是表格吗?
Thanks for the help! 谢谢您的帮助!
UPDATE 1 更新1
I tried passing the array event_ids[]
in the call: 我尝试在调用中传递数组event_ids[]
:
<%= link_to 'Submit ', addinvtimes_invtimes_path(:workorder_id => @workorder, :event_ids[] => event_ids[]), :class => 'btn btn-success' %>
But, got: 但是,得到了:
wrong number of arguments (0 for 1..2)
I just need the array event_ids[]
to be accessible in the controller code addinvtimes
. 我只需要数组event_ids[]
在控制器代码addinvtimes
即可访问。
UPDATE2 更新2
All I'm trying to do is list a bunch of records and include a checkbox column. 我要做的就是列出一堆记录,并包括一个复选框列。 Then in the controller get back a list of ids for the rows that were checked. 然后在控制器中,获取已检查行的ID列表。
The type of the page (index, new, edit, etc.) does not affect the ability to submit data, however, a form is required if you want to post data using pure HTML to be in the params hash. 页面的类型(索引,新建,编辑等)不会影响提交数据的能力,但是,如果要使用纯HTML将数据发布到params哈希中,则需要使用表单。
UPDATE1: UPDATE1:
As for the update, adding :event_ids[] as a parameter to the link will only add it as a querystring parameter to the link, assigning it the value of event_ids[] at the time the view is parsed. 对于更新,将:event_ids []作为参数添加到链接只会将其作为查询字符串参数添加到链接,并在解析视图时为其分配event_ids []的值。 Changes to the checkboxes in the html will not be reflected in the querystring. 对html中复选框的更改不会反映在查询字符串中。 I think, the easiest way would be to just wrap the checkboxes and the submit into a form tag and let it post the data to your controller action. 我认为,最简单的方法是将复选框和提交包装到表单标签中,然后将其发布到控制器操作中。
Oh, and I just saw, the wrong number of arguments error resulted in calling [] on event_ids without an index. 哦,我刚刚看到,错误的参数数量错误导致在没有索引的event_ids上调用[]。 I wasn't sure, but apparently you can't have [] in variable names. 我不确定,但是显然您不能在变量名中使用[]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.