render form only for nested model

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.

 #for fresh property (with review)
 def new
    @property = Property.new       
    @property.property_reviews.build
 end

Below is property model:

class Property < ActiveRecord::Base
  has_many :property_reviews, :dependent => :destroy
  accepts_nested_attributes_for :property_reviews
end

Following are views and partials:

app/views/property/new.html.erb

<%= render :partial => 'form' %>

app/views/property/_from.html.erb

<%= form_for @property, :html => {multipart: true} do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :address, "Complete Address" %>
    <%= f.text_area :address, rows: 3 %>

    <%= f.fields_for :property_reviews do |review| %>
      <%= render "review_fields", r: review %>
    <% end %>

<%= f.submit "Submit", class: "btn btn-large btn-primary" %>

app/views/property/_review_fields.html.erb

<p>
   <%= r.label :comments %>
   <%= r.text_area :comments, rows: 5 %>
   ..
   ..
</p>

Till now things goes fine as I render new for creating a new property with review.

Now I want to add only a review as property address is fixed, controller code for it is given below:

  # add another review to existing property
  def add_new_review
    @property = Property.find(params[:id])
    @review = @property.property_reviews.build
  end

view: app/views/property/add_new_review.html.erb

<b><%= @property.address %></b>
<%= form_for @review do |r| %>
  <%= render "review_fields" %>

  <%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>

When I try to render add_new_review page, I get following error:

undefined method `property_reviews_path' for #<#<Class:0xa2ac3a8>:0xa5e487c>

on line:

<%= form_for @review do |r| %>

This is my first rails project, please let me know what am I doing wrong?

Answers


This is because you don't have defined route for property_review. In order to define standalone path for your model you should write

 resources :property_reviews 

in routes file.

This link will explain you in brief about model_name_path helper,

Models and Nested Forms


Need Your Help

Modifying a running script and having it reload without killing it (php)

php bash shell gnu-screen

I have a gameserver running on Debian where players can edit an (already-running) php script via web to modify the game. However, once changes are made to the script and saved, the affects of the c...

Add missing xts/zoo data with linear interpolation in R

r time-series xts zoo

I do have problems with missing data, but I do not have NAs - otherwise would be easier to handle...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.