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!