开发者

Rails 3, mysql join tables and habtm

I created 2 tables called products and brands and created a join table called brands_products via migration.

In each of the models I wrote the corresponding has_and_belongs_to_many setting.

In a form if have the following code:

<%= form_for(@product) do |f| %>
    <% if @product.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

        <ul>
            <% @product.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
    <% end %>

    <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
    </div>
    <div class="field">
        <%= f.label :bra开发者_C百科nd %><br />
        <%= f.text_field (what to write?) %>
    </div>
    <div class="actions">
        <%= f.submit %>
    </div>
    <% end %>

I don't know how to add a brand to a product intuitively, like the way Rails usually works...any thoughts?


You may want to look at accepts_nested_attributes_for. You'll also want to look at fields_for. Without seeing more of your data model, it's hard to give a detailed answer.

Generally, it would look something like:

<%- f.fields_for :brands do |m| -%>
    <%= m.text_field :name %>
<%- end -%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜