【问题标题】:Polymorphic Association with Comments - Ruby on Rails带有注释的多态关联 - Ruby on Rails
【发布时间】:2010-09-22 00:17:05
【问题描述】:

我在让这些多态关联完全工作时遇到了一些麻烦。我遵循了本教程www.railscasts.com/episodes/154-polymorphic-association,但这似乎只有在我在发布新帖子时是路径 /controller/ID#/cmets 时才有效。如果我尝试在 /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>

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

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

【问题讨论】:

    标签: ruby-on-rails forms polymorphic-associations


    【解决方案1】:

    您的问题是 find_commentable 正在返回 params 哈希本身,因为 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 应该捕获并优雅地处理它们

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多