Topic: Form not saving due to "missing" params.. although they are there

I'm trying to create a new record, but it's not saving due to an error "Name cannot be blank". However, "Name" has a value! Here's the code:

#Controller, nothing fancy here
  def create    
    @accessory = Accessory.new(params[:accessory])

    respond_to do |format|
      if @accessory.save
        format.html { redirect_to accessories_url, notice: 'Accessory was successfully created.' }
        format.json { render json: @accessory, status: :created, location: @accessory }
      else
        format.html { render action: "new" }
        format.json { render json: @accessory.errors, status: :unprocessable_entity }
      end
    end
  end
#Model
validates :name, :presence => true

attr_accessible :full_name

def full_name
  ....
end
#View  (shortened the error loop and removed some fields for brevity)
= form_for(@accessory) do |f|
  - if @accessory.errors.any?
    ...
  .field
    = f.label 'Accessory Name:'
    %br
    = f.text_field :name
  .field
    = f.label 'Description:'
    %br
    = f.text_area :description
  .actions
    = f.submit '+ Add Accessory', {:id => 'submit'}

On the page where I receive the error, I printed the params and got this:

#Again, edited for brevity, only included the "name" and "description" field params
["utf8", "✓"]
["authenticity_token", "+PrcNdOWLFYWxRQdlGHUctzHZgmQPkmvzWxgGlzGfWc="]
["accessory", {"name"=>"Accessory Name", "description"=>"Description - Optional"}]
["commit", "+ Add Accessory"]
["action", "create"]
["controller", "accessories"]

So, the "Name" param IS SET, but I'm getting an error saying it's not. Why?

Last edited by wesf90 (2012-07-13 16:34:55)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Form not saving due to "missing" params.. although they are there

The problem seems to lie in the attr_accessible. If I comment out that line, everything works just as it should. Is it because if I "white list" a single attribute with attr_accessible, I have to white-list all the attributes I want to be accessible?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!