Note that there are some explanatory texts on larger screens.

plurals
  1. POrender form only for nested model
    primarykey
    data
    text
    <p>I have a model named property and a nested model PropertyReviews. I am able to render a single form with property and property_reviews. A property can have multiple reviews, so when I want to add another review, I don't want to render form for property but only for review. Here is my code.</p> <pre><code> #for fresh property (with review) def new @property = Property.new @property.property_reviews.build end </code></pre> <p>Below is property model:</p> <pre><code>class Property &lt; ActiveRecord::Base has_many :property_reviews, :dependent =&gt; :destroy accepts_nested_attributes_for :property_reviews end </code></pre> <p>Following are views and partials:</p> <p>app/views/property/new.html.erb</p> <pre><code>&lt;%= render :partial =&gt; 'form' %&gt; </code></pre> <p>app/views/property/_from.html.erb</p> <pre><code>&lt;%= form_for @property, :html =&gt; {multipart: true} do |f| %&gt; &lt;%= render 'shared/error_messages', object: f.object %&gt; &lt;%= f.label :address, "Complete Address" %&gt; &lt;%= f.text_area :address, rows: 3 %&gt; &lt;%= f.fields_for :property_reviews do |review| %&gt; &lt;%= render "review_fields", r: review %&gt; &lt;% end %&gt; &lt;%= f.submit "Submit", class: "btn btn-large btn-primary" %&gt; </code></pre> <p>app/views/property/_review_fields.html.erb</p> <pre><code>&lt;p&gt; &lt;%= r.label :comments %&gt; &lt;%= r.text_area :comments, rows: 5 %&gt; .. .. &lt;/p&gt; </code></pre> <p>Till now things goes fine as I render new for creating a new property with review.</p> <p>Now I want to add only a review as property address is fixed, controller code for it is given below:</p> <pre><code> # add another review to existing property def add_new_review @property = Property.find(params[:id]) @review = @property.property_reviews.build end </code></pre> <p>view: app/views/property/add_new_review.html.erb</p> <pre><code>&lt;b&gt;&lt;%= @property.address %&gt;&lt;/b&gt; &lt;%= form_for @review do |r| %&gt; &lt;%= render "review_fields" %&gt; &lt;%= f.submit "Submit", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>When I try to render add_new_review page, I get following error:</p> <pre><code>undefined method `property_reviews_path' for #&lt;#&lt;Class:0xa2ac3a8&gt;:0xa5e487c&gt; </code></pre> <p>on line:</p> <pre><code>&lt;%= form_for @review do |r| %&gt; </code></pre> <p>This is my first rails project, please let me know what am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload