Ruby on Rails simple_form_for all.each 做
我正在使用 Rails 3.0、Ruby 1.9.2 和 Plataformatec simple_form gem。此代码适用于 form_for 但不适用于 simple_form_for:
<%= simple_form_for(@provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, @store.cars.include?(c), :name => 'store[car_ids][]' %>
$<%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
如何让它与 simple_form_for 一起使用?
提前致谢!
I am using Rails 3.0, Ruby 1.9.2 and the Plataformatec simple_form gem. This code works with a form_for but not simple_form_for:
<%= simple_form_for(@provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, @store.cars.include?(c), :name => 'store[car_ids][]' %>
lt;%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
How do I get it to work with simple_form_for?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能像
form_for
一样使用simple_form
。例如,
simple_form
gem 中没有任何check_box_tag
方法。您只能使用:as
选项指定inuput
字段。因此,您的check_box_tag
将转换为Checkout 使用情况、Rdoc 和其他有用的内容 https://github .com/plataformatec/simple_form
You can't use
simple_form
right the same way asform_for
.For example ther is no any
check_box_tag
method insimple_form
gem. There is ONLYinuput
fields that you can specify with:as
option. So yourcheck_box_tag
will be converted toCheckout Usage, Rdoc and other useful stuff https://github.com/plataformatec/simple_form
问题出在控制器代码中。
在“新”控制器操作中,我不能
像通常那样简单地执行。
相反,我必须单独处理每个参数:
@provider.location = params[:provider][:location]
等等......
对于汽车复选框,我将 car_ids 参数中的每个 car_id 一次添加到“has_many”汽车模型关联中:
然后我可以调用:
并且它保存正确(我最初的问题是它不是保存选定的汽车)。
出于某种原因,我只有在这里发布问题后才能弄清楚这一点。有趣的是,这是如何运作的。
谢谢大家的回复!
The problem was in the controller code.
In the "new" controller action I can't simply perform:
as one would normally.
Instead I have to process each parameter separately:
@provider.location = params[:provider][:location]
etc...
For the Car check boxes, I add each car_id from the car_ids parameter to the "has_many" cars model association one at a time:
Then I can call:
And it saves correctly (my initial problem was that it wasn't saving the selected Cars).
For some reason, I was able to figure this out only after posting the question here. Funny how that works.
Thanks all for your replies!