2012-07-25 47 views
3

我已经阅读了大量的帖子,但仍无法确定这一点。使用accep_nested_attributes_for和polymorphico时不能批量分配受保护的属性

我有一个forum_post模型和一个链接模型。我想将链接表单与forum_post表单嵌套,但不断地得到一个无法批量分配受保护的属性:链接。

ForumPost型号

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

    has_many :links, :as => :linkable, :dependent => :destroy 

    accepts_nested_attributes_for :links, :allow_destroy => true 
end 

链接模式

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

    belongs_to :linkable, :polymorphic => true 
end 

Forum_post查看

<%= form_for(@forum_post) do |f| %> 
    <% if @forum_post.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@forum_post.errors.count, "error") %> prohibited this forum_post from being saved:</h2> 

     <ul> 
     <% @forum_post.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :content %><br /> 
    <%= f.text_area :content, :rows => 5 %> 
    </div> 

    <%= f.fields_for :link do |link| %> 
    <%= render :partial => 'links/link', :locals => { :f => link} %> 
    <% end%> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

链接查看部分

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

<div id="link_preview"> 
</div> 

ForumPosts控制器

class ForumPostsController < ApplicationController 

    def new 
    @forum_post = ForumPost.new 

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

    def create 
    @forum_post = ForumPost.new(params[:forum_post]) 

    respond_to do |format| 
    if @forum_post.save 
     format.html { redirect_to @forum_post, notice: 'Forum post was successfully created.' } 
     format.json { render json: @forum_post, status: :created, location: @forum_post } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @forum_post.errors, status: :unprocessable_entity } 
    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.links.build(params[:link]) 
    if @link.save 
     flash[:notice] = "Successfully saved link." 
     redirect_to :id => nil 
    else 
     render :action => 'new' 
    end 
    end 

end 
+0

'ForumPostsController'的create方法在哪里? – deefour 2012-07-25 17:22:01

+0

你是否从'ForumPostsController#create','LinksController#create'或两者获得警告?我也没有在'ForumPostsController#new'中看到'@ forum_post.links.build',这是故意的吗? – HargrimmTheBleak 2012-07-25 17:31:48

+0

错误消息仅在ForumPostsController#create上。我已经尝试将@ forum_post.links.build添加到ForumPostsController#new,但仍然得到相同的错误 – otissv 2012-07-25 18:27:07

回答

1

那么,根据你的问题被保护的属性,你可以按质量不分配是:链接。 不知道如何发生,但你有没有尝试attr_accessible:链接?

至于安全隐患,这是github被黑了一次https://gist.github.com/1978249的原因,我非常不鼓励将whitelist_attributes设置为false。

相关问题