ruby-on-rails - 使用accepts_nested_attributes_for和多态时,无法批量分配 protected 属性

标签 ruby-on-rails nested-attributes polymorphism

我在这里阅读了很多帖子,但仍然无法弄清楚。

我有一个forum_post模型和一个链接模型。我想将链接表单与forum_post表单嵌套在一起,但继续获取“无法批量分配 protected 属性:链接”。

论坛帖子模型

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

论坛_帖子查看
<%= 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>

论坛帖子 Controller
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

链接 Controller
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

最佳答案

好吧,根据您的问题,您无法批量分配的 protected 属性是:links。
不确定如何发生,但是您是否尝试过attr_accessible:links?

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

关于ruby-on-rails - 使用accepts_nested_attributes_for和多态时,无法批量分配 protected 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11655098/

相关文章:

c++ - TwoDimensionalShape 类应该包含什么?

c# - 如何检查对象是否派生自接口(interface)?

ruby-on-rails - 设计+主动管理员重定向

ruby-on-rails - 设备确认后自动登录

ruby-on-rails - 在 Rails API token 身份验证中,www-Authenticate header 的领域 ="Application"部分是什么?

ruby-on-rails - 如何在 before_update 回调中为 activerecord 嵌套属性处理新子级

ruby-on-rails - 工厂女孩多重嵌套属性

javascript - 在 ruby​​ on rails 5 应用程序中使用带有 Materialize Css 和简单表单的 Cocoon Gem 时出现日期选择器问题

select - 找不到名称关联-Nested Form Rails 4

java - 车辆类别(不同类型)。最佳设计与实现