2

Rails 新手,这个问题困扰了我一段时间。所有关于多态关联的教程似乎只有两个层次,而且从来没有嵌套在多态模型中的多态模型。真的需要帮助。

我有一个具有帖子、评论和链接模型的应用程序。评论和链接都是多态的,因为我想将链接 url 添加到评论和帖子,以及对其他事物的评论,类似于 facebook 的工作方式。我想嵌套链接模型,以便可以作为一种形式发送。发布和链接有效,但评论和链接出现 MassAssignmentSecurity 错误。

错误

ActiveModel::MassAssignmentSecurity::Error in CommentsController#create
Can't mass-assign protected attributes: link

请求参数:

{"utf8"=>"✓",
 "authenticity_token"=>"

J1JfIINrtvax77M3JcbDDWvHFpyGG1ciK1DisGOLu6M=",
 "comment"=>{"content"=>"z",
 "link"=>{"link_url"=>"z"}},
 "commit"=>"Add comment",
 "forum_post_id"=>"16"}

路线

resources :forum_posts do
  resources :comments
  resources :links
end

resources :comments do
  resources :links
end

论坛_帖子模型

class ForumPost < ActiveRecord::Base
  attr_accessible :content, :links_attributes, :comments_attributes , :link_url

  has_many :links, :as => :linkable
  has_many :comments, :as => :commentable

  accepts_nested_attributes_for :links, :allow_destroy => true  #, :reject_if => lambda { |t| t[:link].nil?}
end

评论模型

class Comment < ActiveRecord::Base
  attr_accessible :commentable_id, :commentable_type, :content,:links_attributes, :link_url

  belongs_to :commentable, :polymorphic => true

  has_many :links, :as => :linkable

  accepts_nested_attributes_for :links, :allow_destroy => true  #, :reject_if => lambda { |t| t[:link].nil?}
end

链接模型

class Link < ActiveRecord::Base
  attr_accessible :description, :image_url, :link_url, :linkable_id, :linkable_type, :title, :link_id

  belongs_to  :linkable, :polymorphic => true
end

论坛_帖子控制器

class ForumPostsController < ApplicationController
...............
def new
    @forum_post = ForumPost.new
    @link = @forum_post.links.build


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @forum_post }
    end
  end
........
end

评论控制器

class CommentsController < ApplicationController

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

  def index
    @commentable = find_commentable
    @comments = @commentable.comments
  end

  def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.links.build
    if @comment.save
      flash[:notice] = "Successfully saved comment."
      redirect_to :id => nil
    else
      render :action => 'new'
    end
  end

end

链接控制器

class LinksController < ApplicationController

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

  def index
  @linkable = find_linkable
    @links = @linkable.links
  end

  def create
    @linkable = find_linkable
    @link = @linkable.link.build(params[:link])
    if @link.save
      flash[:notice] = "Successfully saved link."
      redirect_to :id => nil
    else
      render :action => 'new'
    end
  end
end

评论部分视图

<h2>Comments</h2>

<% if commentable.comments.empty? %>
  No comments to display.
<% else %>
  <% for comment in commentable.comments %>
    <%= comment.content %>
  <% end %>
<% end %>

<% :link_url %>

<h2>New Comment</h2>
<%= form_for [@commentable, Comment.new] do |f| %>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content, :rows => 5 %>
  </div>


  <%= f.fields_for [@linkable, Link.new]  do |link| %>
   <%= render :partial => 'links/link', :locals => { :f => link } %>
  <% end%>

  <div class="actions">
    <%= submit_tag "Add comment" %>
  </div>
<% end %>

链接部分视图

<h2>Link Previews</h2>

<div class="field">
  <%= f.label :link_url %><br />
  <%= f.text_field :link_url, :id => "url_field" %>
</div>
4

0 回答 0