0

I'm just banging my head on the wall because of Rails 4 strong parameters.

Basically, I have a very simple model:

class Band < ActiveRecord::Base
  validates_presence_of :name
  has_many :users
  has_many :donations
  attr_accessible :name #I'm using protected_attributes
end

The user must be logged in order to create a band, and when it comes to create one, here it is my controller

def new
  @band = Band.new
end

def create
 @band = Band.new(band_params)

 respond_to do |format|
  if @band.save

    format.html { redirect_to @band, notice: 'La band è stata creata' }
    format.json { render action: 'show', status: :created, location: @band }
  else
    format.html { render action: 'new' }
    format.json { render json: @band.errors, status: :unprocessable_entity }
  end
 end
end

private

def set_band
  @band = Band.find(params[:id])
end

def band_params
   params.require(:band).permit(:name)
end

With this configuration there's just no way to create a new Band object, not even from the console!!!

b = Band.new
b.save
=> false
b.errors.messages
=> {:name=>["can't be blank"]}
b.band = "band's name"
b.save
=> false
b.errors.messages
=> {}
Band.create!(name: "OneName")
WARNING: Can't mass-assign protected attributes for Band: name
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

What is happening? I really have no clue!

e4r
  • 39
  • 3
  • 1
    Strong params won't be affecting your console session, it's defined in your controller after all. Have you tried it through a webserver, or with a fresh console? Also, I assume `b.band = "band's name"` was a typo? – Matt Oct 30 '13 at 15:28
  • Problem is I can't add new records at all, whether I'm using the console or not. On the web view simply nothing happens, the page reloads with params passed and the validation error shown. – e4r Oct 30 '13 at 17:19

0 Answers0