I'm trying to combine two form inputs into one database entry parameter. In my form I have dropdown for hours (6-22) and for mins (0-55) and in my db I have a column 'start' where I would like to have an integer calculated as start_hour * 60 + start_mins.
My form looks like that:
<div class="form-group">
<%= f.label :start_hour %>
<%= f.select :start_hour, 6 .. 22, class: 'form-control' %>
<%= f.label :start_mins %>
<%= f.select :start_mins, 00 .. 55, class: 'form-control' %>
</div>
In my model I added before_validation:
before_validation :generate_starttime
def generate_starttime
self.start= '#{:start_hour}' * 60 + '#{start_mins}'
end
and I have a problem with my controller - I don't know how to pass start into the db. I'm passing params as:
def create
@klass = Klass.new(contact_params)
if @klass.save
flash[:success] = "Class added"
else
flash[:error] = @klass.errors.full_messages.join(", ")
end
redirect_to new_klass_path
end
and I tried self.start and @start and :start and nothing really works - but I admit I stay in the dark. For now I receive and error:
undefined method `start' for # Did you mean? status
for "self.start" version and
undefined method `start_mins' for # Did you mean? start start_was
for ":start" and "@start" version :D I feel like I'm missing some very basics here. Could you point me into the right direction?
If that is important that's my db schema:
create_table "klasses", force: :cascade do |t|
t.string "name"
t.string "teacher"
t.string "day"
t.integer "start"
t.integer "duration"
end