0

what i need to made to make the create action works?

i hava a table with a has_and_belongs_to_many association. The "new" page works fine, but when i select itens and try to save it raises an error:

Can't mass-assign protected attributes: book_id

I tryed to set:

config.active_record.whitelist_attributes = false

but it didnt change anything

My model:

class Book < ActiveRecord::Base
  has_and_belongs_to_many :orbs

  attr_accessible :dataf, :datai, :descr, :nome

  validates :nome, uniqueness: true, presence: true
end

class Orb < ActiveRecord::Base
  belongs_to :orb_type
  has_and_belongs_to_many :books

  attr_accessible :descr, :nome, :orb_type_id

  validates :nome, uniqueness: true, presence: true
end

My controler:

  def create
    @orb = Orb.new(params[:orb])

    respond_to do |format|
      if @orb.save
        format.html { redirect_to @orb, notice: 'Orb was successfully created.' }
        format.json { render json: @orb, status: :created, location: @orb }
      else
        format.html { render action: "new" }
        format.json { render json: @orb.errors, status: :unprocessable_entity }
      end
    end
  end

Also, anyone can tell me what i will have to do to make the autoboxes beeing checked when o press "edit" i am new to rails. Thx!

Edit: adding attr_accessible :book_id to my orb model raise the error:

unknown attribute: book_id

It worked on the console with << operation.

Techmago
  • 380
  • 4
  • 18

1 Answers1

0

You need to declare book_id as accessible on your model Orb

attr_accessible :descr, :nome, :orb_type_id, :book_id

When you edit an object, if the name of the checkbox for a boolean value is correct (same as the db field), it should show on the view as checked or unchecked based on its value.

aruanoc
  • 817
  • 1
  • 7
  • 9
  • Doing that i get: unknown attribute: book_id – Techmago Sep 17 '13 at 19:19
  • Is book_id a field on the corresponding DB table? – aruanoc Sep 17 '13 at 19:27
  • Huh i found the error. I got to do what you told me and use the correct thing in the _form. i was doing <%= check_box_tag "orb[book_id][]", book.id, @orb.books.include?(book) %> instead <%= check_box_tag "orb[book_ids][]", book.id, @orb.books.include?(book) %> a little "s" missing. think it is it. thx! – Techmago Sep 17 '13 at 19:32