0

我希望能够分别为这两个模型撰写评论(使用具有嵌套关联的多态路径)。目前,我有一个问题,如果我想为城市写评论,那么记录是为国家创建的,而不是为城市创建的


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

为国家创造了良好的概述,

=> #<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">

但:

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

再次为国家创建审查,而不是为城市创建

=> [#<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">]

请告诉我,如何使用带有嵌套关联的多态路径为城市创建评论?


我的路线.rb:

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

审查控制人

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

评论索引

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

和形式

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

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

提前致谢!

4

0 回答 0