2

I have some nested models in my Rails application. i have an article hat has mny properties.

class Article < ActiveRecord::Base
  has_many :properties, :dependent => :destroy
  accepts_nested_attributes_for :properties
end

class Property < ActiveRecord::Base
  belongs_to :article
end

And now i want to edit this in my View so I editet the controler

  # GET /articles/new
  # GET /articles/new.json
  def new
    @article = Article.new
    3.times { @article.properties.build }

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @article }
    end
  end

And also edited the View and the _format.html.erb

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

      <ul>
      <% @article.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 :description %><br />
    <%= f.text_area :description %>
  </div>
  <% f.fields_for :properties do |prop| %>
            <div class="field">
            <%= prop.label :name %><br />
            <%= prop.text_field :name %>
         </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

But there is no way to show up. If i want to create a new Model, i can not see any input fields for the properties.

What have i do wrong?

1 Answer 1

4

You're missing an = in your fields_for line. That is, it should be:

<%= f.fields_for :properties do |prop| %>
Sign up to request clarification or add additional context in comments.

1 Comment

No worries. Don't forget you can also Accept this as the correct answer by clicking the green check-mark; raising your accepted rate will make others more willing to help you in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.