Ruby on rails Rails多态到嵌套多态质量分配安全错误
对rails来说是个新手,这个问题困扰了我一段时间。所有关于多态关联的教程似乎只有两个级别,而且从来没有嵌套在多态模型中的多态模型。我真的需要帮助 我有一个应用程序,有帖子,评论和链接模型。评论和链接都是多态的,因为我想将链接URL添加到评论和帖子,以及其他内容的评论,类似于facebook的工作方式。我想嵌套链接模型,以便可以作为一个表单发送。帖子和链接可以工作,但是评论和链接有一个MassaSignmentSecurity错误 厄洛尔 请求参数:Ruby on rails Rails多态到嵌套多态质量分配安全错误,ruby-on-rails,polymorphism,nested-attributes,Ruby On Rails,Polymorphism,Nested Attributes,对rails来说是个新手,这个问题困扰了我一段时间。所有关于多态关联的教程似乎只有两个级别,而且从来没有嵌套在多态模型中的多态模型。我真的需要帮助 我有一个应用程序,有帖子,评论和链接模型。评论和链接都是多态的,因为我想将链接URL添加到评论和帖子,以及其他内容的评论,类似于facebook的工作方式。我想嵌套链接模型,以便可以作为一个表单发送。帖子和链接可以工作,但是评论和链接有一个MassaSignmentSecurity错误 厄洛尔 请求参数: {"utf8"=>"✓", "aut
{"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
评论管理员
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
链路控制器
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>
链接局部视图
<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>
有人能帮忙吗?你有解决过这个问题吗?我有类似的情况,但问题不同。当链接可以是帖子或评论时,我不知道如何将_重定向到@linkable。多态url帮助器需要父模型来进行注释,但很明显,帖子没有父模型。是的,最终它成功了。我所要做的就是在post控制器的show动作中呈现commnet和links。这里有一个例子,有人能帮上忙吗?你解决过这个问题吗?我有类似的情况,但问题不同。当链接可以是帖子或评论时,我不知道如何将_重定向到@linkable。多态url帮助器需要父模型来进行注释,但很明显,帖子没有父模型。是的,最终它成功了。我所要做的就是在post控制器的show动作中呈现commnet和links。这里有一个例子