2

我在让这些多态关联完全工作时遇到了一些麻烦。我遵循了本教程www.railscasts.com/episodes/154-polymorphic-association,但这似乎只有在我发布新帖子时是路径 /controller/ID#/comments 时才有效。如果我尝试在 /controller/ID# 上呈现部分评论表单,我会在创建评论时收到此错误:

undefined method `comments' for #<ActiveSupport::HashWithIndifferentAccess:0x10341b2d0>

我有三个模型:

class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true       
end


class Post < ActiveRecord::Base
    belongs_to :user
    has_many :comments, :as => :commentable
end

class Article < ActiveRecord::Base
    belongs_to :user
    has_many :comments, :as => :commentable
end

这是我对 /articles/#{ID} 的看法

<%= @article.title %>
<%= @article.content %>
<%= @article.user.login %>
<%= link_to 'Edit Article', edit_article_path(@article) %>

<h2>Comments</h2>
<%= render "comments/comments" %>
<%= render "comments/comment" %>

这是我的部分评论:

<div class="post_comment">
<%= form_for [@commentable, Comment.new] do |f| %>
    <%= f.text_area :content %>
    <%= f.submit "Post" %>
<% end %>
</div>

这是我在评论控制器中的创建方法:

def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.user_id = current_user.id
    if @comment.save
        redirect_to :id => nil
    else    
        flash[:notice] = "something went wrong"
        redirect_to @commentable
    end 
end

def find_commentable
    params.each do |name, value|
        if name =~ /(.+)_id$/
            return $1.classify.constantize.find(value)
        end
    end
end

我想我了解问题所在,但不确定如何解决问题。此表单正在寻找@commentable,但如果路径未嵌套,则无法找到@commentable(可能是错误的)。

这是我的路线:

devise_for :users do
        get "login", :to => "devise/sessions#new"
        get "register", :to => "devise/registrations#new"
    end

    resources :posts do
        resources :comments
        resources :tags
    end

    resources :articles do
        resources :comments
        resources :tags
    end

    resources :users do 
        resources :articles
        resources :comments
        resources :tags
        resources :posts
    end 

    resources :comments

    root :to => "home#index"

end
4

2 回答 2

3

您的问题是 find_commentable 正在返回参数哈希本身,因为 return 语句从未触发。

我通过错误消息推断出这一点,因为params.class == ActiveSupport::HashWithIndiffrentAccess

def find_commentable
    params.each do |name, value|
        if name =~ /(.+)_id$/
            return $1.classify.constantize.find(value)
        end
    end
end

我会添加类似的东西

def find_commentable
    params.each do |name, value|
        if name =~ /(.+)_id$/
            return $1.classify.constantize.find(value)
        end
    end
    raise ActiveRecord:NoRecord.new("Couldn\'t find it captain!")
end

我认为上述异常类型是正确的,如果没有看到 Comment.find_by_id(-1) 或类似的东西会引发什么。应该是类似的东西。

编辑

我建议提出异常的原因是,当人们开始四处寻找并访问 example.com/posts/9001 时,ApplicationController 应该捕获并优雅地处理它们

于 2010-12-30T01:11:07.763 回答
0
devise_for :users do
        get "login", :to => "devise/sessions#new"
        get "register", :to => "devise/registrations#new"
    end

    resources :posts do
        resources :comments
        resources :tags
    end

    resources :articles do
        resources :comments
        resources :tags
    end

    resources :users do 
        resources :articles
        resources :comments
        resources :tags
        resources :posts
    end 

    #REMOVE IT resources :comments

    root :to => "home#index"

end

并添加

def index
    @commentable = find_commentable #this
    @comments = @commentable.comments

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end
于 2011-10-28T10:26:13.830 回答