Ruby On Rails 3 - Form - Get nested data that are in somehow connected to the form?
I have a form with开发者_如何学C 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
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:
@category = YourCategory
@product = YourProduct
@size = YourSize
Then in your form somewhere you can just do something like:
<%=h @category.name %>
<%= Here I want some code showing the name of the product %>
is now
<%= pr.object.name %>
note 'object' does not represent something. just use as typed
精彩评论