如何将 Rails 3 表单参数传递到数组中?
我有一个嵌套表单(在相应模型中使用accepts_nested_attributes_for):
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<%= f.fields_for :service do |s| %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<% end %>
<% end %>
如果我只有一个 s.text_field,则效果很好。但是一旦我添加额外的文本字段,它就无法正常运行。如果你看一下源代码,六个人的 id 和 name 都是一样的?
如何将这些参数放入数组中? [这样我就可以像这样隔离它们:]
service1 = Service.named(params[:technician][:service][1][:name])
(我已经尝试过railscasts第192集描述的方法,但它也不起作用)。
I have a nested form (using accepts_nested_attributes_for in respective models):
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<%= f.fields_for :service do |s| %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<% end %>
<% end %>
This works fine if I only have one s.text_field. But once I add additional text_fields, it doesn't run properly. If you look at the source code, the id and name are the same for all six?
How do I put these params into an array? [so that I can isolate them like this:]
service1 = Service.named(params[:technician][:service][1][:name])
(I've tried the method described on railscasts episode 192, but it didn't work either).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几个小时的反复试验,我已经破解了一些有效的方法(但是如果您知道更好、更雄辩的方法,请告诉我):
in view:
in control:
确保将 i 转换为 string
After hours of trial and error, I've hacked something that works (but please let me know if you know a better, more eloquent way):
in view:
in controller:
make sure you turn i into string