在我的rails应用程序中,我需要一些关联方面的帮助。得到一个“无法批量分配受保护的属性: rss_readers”的警告,并且不知道问题出在哪里。
class Scraper < ActiveRecord::Base
attr_accessible :name, :link, :rss_reader_attributes
has_one :rss_reader
accepts_nested_attributes_for :rss_reader和accociation:
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 %>我以为这没问题,但我得到了警告。有谁有主意吗?
谢谢
发布于 2012-02-16 11:15:13
基于this,您可能需要显式地将RssReader添加到:attr_accessible。
发布于 2012-02-17 02:07:03
基本上,当你说某个东西是属性可访问的,那么你就不能批量分配那个特定的属性……所以你得到的错误是正确的。您不能执行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的更好想法,请参阅此文档
https://stackoverflow.com/questions/9303372
复制相似问题