I have a problem with the mass-assignment and strong parameters. In my model I have several attributes, that are represented through a column in my mysql database. I also have a field, that isn't. I just need it to calculate a value. I don't want to store it in my database.
In my controller I defined the strong parameters:
def timeslots_params
params.require(:timeslot).permit(:employee_id, :dienstplan_id, :start_date, :start_time)
end
But when I'm trying to access the start_time attribute in my controller, I'm getting the
undefined method `start_time' for #<Timeslot:0x007fff2d5d8070> error.
The other defined parameters with db columns are present and filled with values. Do I missunderstand the strong parameters and have to define something else?
EDIT: Here is the code, where I call the timeslots_params:
def create
@e = Timeslot.new(timeslots_params)
@e.start_date = @e.start_date.to_s + " " + @e.start_time.to_s
if @e.save
current_user.timeslots << @e
respond_to do |format|
format.json { render :json => @e }
end
end
end
Please, permit only the params you expect your user send with data. If start_time is not user data for update your db, use this way:
params.require(:timeslot).permit(:employee_id, :dienstplan_id, :start_date)
Strong parameters prevents save data that user sends and you don't want he/she update.
If you use :start_time, it must be defined at your model.
Ops, I've seen your update:
@e.start_date = @e.start_date.to_s + " " + @e.start_time.to_s
If you send :start_time to an Timeslot instance them start_time must be a method of Timeslot model. Defined by rails if it is a db field, defined with attr_accesor or att_reader or defined by a def key on your source model.
If not @e.start_time
trigger undefined method 'start_time'
.
edited again:
Now, start_time is a model variable. Make sure it is send to the form in the same way that you do with the fields. By default we use an f <%= f.text_field :the_field %>
. Just don't forget the f.
Now you must permit this field again, if you follow my first advice.