在我的rails应用程序中需要一些关联帮助。获取“无法批量分配受保护的属性:rss_readers”警告并且不知道问题是什么。
class Scraper < ActiveRecord::Base
attr_accessible :name, :link, :rss_reader_attributes
has_one :rss_reader
accepts_nested_attributes_for :rss_reader
和协会:
class RssReader < ActiveRecord::Base
attr_accessible :title, :address, :content
belongs_to :scraper
在rails控制台上,它可以正常工作。
> scraper = Scraper.new
> scraper.build_rss_reader
> scraper.attributes={:rss_reader_attributes=>{:address => "asdsad"}}
但是在控制器中我得到了警告。
def new
@scraper = Scraper.new
@scraper.build_rss_reader
end
def create
@scraper = Scraper.new(params[:scraper])
@scraper.build_rss_reader
if @scraper.save
redirect_to :show
else
render :new
end
那就是新观点
<%= form_for(@scraper) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for(@scraper.rss_reader) do |rss| %>
<div class="field">
<%= rss.label :address %><br />
<%= rss.text_field :address %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
我认为没事,但我得到了警告。有人有想法吗?
由于
答案 0 :(得分:2)
根据this,您可能需要明确将RssReader
添加到:attr_accessible
。
答案 1 :(得分:2)
基本上,当你说某些属性可访问时,你就无法批量分配该特定属性......所以你得到的错误是对的。 你不能做object.update_attributes
what you can try is do
@rssreader = rssreader.new
@rssreader.address = 'the address'
and then
@scrapper.rssreader = @rssreader
请参阅此内容,以便更好地了解attr_accessible Rails mass assignment definition and attr_accessible use