0

I want to be able to write a review for the two models separately (using polymorphic paths with nested associations). At the moment, I have a problem in that if I want to write a review for the city, then the record is created for the country, not for the city


http://localhost:3000/countries/1/reviews/new

good overview is created for the country,

=> #<Review id: 1, content: "", reviewable_id: 1, reviewable_type: "Country", created_at: "2013-03-15 18:57:15", updated_at: "2013-03-15 18:57:15">

but:

http://localhost:3000/countries/1/cities/1/reviews/new

again review created for the country, not for the city

=> [#<Review id: 2, content: "second", reviewable_id: 1, reviewable_type: "Country", created_at: "2013-03-15 19:00:34", updated_at: "2013-03-15 19:00:34">]

Tell me please, how do I get to create a review for the city using using polymorphic paths with nested associations?


my routes.rb:

  resources :countries do
    resources :reviews
    resources :cities do
      resources :reviews
    end
  end

review controller

class ReviewsController < ApplicationController
  load_resource :country
  load_resource :city
  load_and_authorize_resource :review, :through => [:country, :city]

  def new; end

  def create
    respond_to do |format|
      if @review.save
        format.html { redirect_to @review, notice: 'Review was successfully created.' }
        format.json { render json: @review, status: :created, location: @review }
      else
        format.html { render action: "new" }
        format.json { render json: @review.errors, status: :unprocessable_entity }
      end
    end
  end

  ....
end

reviews index

%h1 Listing reviews
= link_to 'New Review', new_polymorphic_path([@country, @city, :review]) if can? :new, Review

and form

= form_for(@review, url: polymorphic_path([@country, @city, @review])) do |f|

  .field
    = f.label :content
    %br/
    = f.text_area :content
  .actions
    = f.submit

Thanks in advance!

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
nilid
  • 372
  • 2
  • 12
  • Can you add this line in the html.erb <%= params %> and post the result here? – beck03076 Mar 15 '13 at 22:43
  • 2
    Your nested resources should not have more than 1 level deep. The reason why is here: http://weblog.jamisbuck.org/2007/2/5/nesting-resources – Pigueiras Mar 16 '13 at 00:04
  • I suggest you use shallow routes for cities so that your nested resources are not deeper than 1 level. Also, the generated path helpers will have sane names. – Salil Mar 16 '13 at 07:03
  • for beck03076: {"action"=>"new", "controller"=>"reviews", "country_id"=>"1", "city_id"=>"1"} – nilid Mar 16 '13 at 08:34
  • @nilid Did you manage to solve this problem ? I got the very same problem. – sarunw Dec 23 '13 at 08:07

0 Answers0