Ruby On Rails 3 - 表单 - 获取以某种方式连接到表单的嵌套数据?
我有一个包含类别、名称、尺寸、价格和网址的表格。
前 3 项已填写,您应该无法更改该数据。
现在我想在输入框中显示名称而不是category_id、name、size_id。我该如何实现这个目标?
到目前为止,我得到的最接近的是使用 @size.map |s|... 创建一个“选择”输入,但这不是一个好的解决方案。我不希望用户能够更改这些数据,所以隐藏字段是我认为最好的解决方案吗?但我希望能够向他们展示有关类别、名称和尺寸的信息。
<%= f.fields_for :price do |pr| %>
<tr>
<td><%= pr.hidden_field :product_id %> <%= Here I want some code showing the name of the product %></td>
<td><%= pr.text_field :price %></td>
<td><%= pr.text_field :url %></td>
<td><%= pr.check_box :_destroy %></td>
</tr>
<% end %>
在我看来我有这些
@size = ProductSize.all
@products = Product.all
@categories = Category.all
I have a form with category, name, size, price and url.
The first 3 have already been filled in and you shouldn't be able to change that data.
Now I want to display the names instead of category_id, name, size_id in a input box. How do I accomplish this?
The nearest I've gotten so far is to create a "select" input with @size.map |s|.... but that is not a good solution. I don't want the user to be able to change these data, so a hidden_field is the best solution I think? But I want to be able to show them the information about the category, name and size.
<%= f.fields_for :price do |pr| %>
<tr>
<td><%= pr.hidden_field :product_id %> <%= Here I want some code showing the name of the product %></td>
<td><%= pr.text_field :price %></td>
<td><%= pr.text_field :url %></td>
<td><%= pr.check_box :_destroy %></td>
</tr>
<% end %>
In my views I have these
@size = ProductSize.all
@products = Product.all
@categories = Category.all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我完全理解你的控制器是如何工作的,但无论如何你应该能够获得产品、尺寸和类别。如果您能够设置它们的 ID,那么您可能已经获得了它们。在您的控制器中,只需将产品、尺寸和类别设置为变量即可。
所以,在你的控制器中:
然后在你的表单中的某个地方你可以做类似的事情:
I'm not sure I fully understand how your controller works, but regardless you should be able to get the product, size, and category. You probably have already got them if you were able to set their IDs. In your controller just set the product, size, and category to a variable.
So, in your controller:
Then in your form somewhere you can just do something like:
现在
请注意“对象”并不代表某些东西。只需按键入的方式使用
is now
note 'object' does not represent something. just use as typed