Rails多态到嵌套的多态质量分配安全性错误

时间:2012-10-05 13:14:19

标签: ruby-on-rails polymorphism nested-attributes

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

我有一个包含帖子,评论和链接模型的应用。评论和链接都是多态的,因为我想在评论和帖子中添加链接网址,以及对其他内容的评论,类似于facebook的工作原理。我想嵌套链接模型,因此可以作为一个表单发送。发布和链接工作但注释和链接有MassAssignmentSecurity错误。

Erorr

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

Forum_Post模型

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

Forum_Post控制器

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>

0 个答案:

没有答案