mibo
mibo

Reputation: 73

can't mass-assign protected attribute

need some help with some associations in my rails app. Get a "can't mass-assign protected attributes: rss_readers" warning and don't figured out what's the problem.

class Scraper < ActiveRecord::Base
  attr_accessible :name, :link, :rss_reader_attributes

  has_one :rss_reader
  accepts_nested_attributes_for :rss_reader

And the accociation:

class RssReader < ActiveRecord::Base
  attr_accessible :title, :address, :content 

  belongs_to :scraper

At the rails console its works fine.

> scraper = Scraper.new 
> scraper.build_rss_reader 
> scraper.attributes={:rss_reader_attributes=>{:address => "asdsad"}}

But in controller i get the warning.

  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

And thats the new view

<%= 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 %>

I thougth that's all right but i get the warning. Anyone have an idea?

Thanks

Upvotes: 1

Views: 1588

Answers (2)

Ross
Ross

Reputation: 1562

Basically when you say something to be attribute accessible, then you cannot mass assign that particular attribute ... so the error you are getting is right. You cannot do object.update_attributes

what you can try is do 
@rssreader = rssreader.new
@rssreader.address = 'the address'
and then 
@scrapper.rssreader = @rssreader

Refer this for a better idea on attr_accessible Rails mass assignment definition and attr_accessible use

Upvotes: 2

ScottJShea
ScottJShea

Reputation: 7111

Based on this you may need to explicitly add RssReader to :attr_accessible.

Upvotes: 2

Related Questions