Tony Vincent
Tony Vincent

Reputation: 14272

Save rails time_select as string

I have a string field in my databse

class CreateMHolidays < ActiveRecord::Migration
  def change
    create_table :m_holidays do |t|
      t.string :open_schedule, :limit => 50
    end
  end
end

I am using time_select to get the value for open_schedule field.

<%= f.time_select :open_schedule, {minute_step: 01, include_blank: true,:default =>{:hour => '00', :minute => '00'},:ignore_date => true}, {:class => 'form-control'} %>

In my controller I try

@m_holidays = MHoliday.new(m_holiday_params)

@open_schedule_hrs = (params[:m_holidays]['open_schedule(4i)']).to_s
@open_schedule_mns = (params[:m_holidays]['open_schedule(5i)']).to_s
@m_holidays.open_schedule = @open_schedule_hrs + ':' + @open_schedule_mns

But when I try to save the record I am getting

ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment of multiparameter attributes [error on assignment [3, 3] to open_schedule (Missing Parameter - open_schedule(1))])

This is the first time I am using time_select and I must use it with a string field rather than :time. How to go about this? Any help much appreciated

Upvotes: 1

Views: 492

Answers (1)

user1351104
user1351104

Reputation:

You're getting the ActiveRecord::MultiparameterAssignmentErrors because of the mass parameter assignment on the line @m_holidays = MHoliday.new(m_holiday_params). This might be due to m_holiday_params containing parameters that your MHoliday model doesn't know what to do with.

Try filtering out everything related to the open_schedule input from m_holiday_params. If you have an m_holiday_params method like this:

def m_holiday_params
  params.require(:m_holiday).permit('open_schedule(4i)', 'open_schedule(5i)', ...)
end

then omit the open_schedule parameters:

def m_holiday_params
  params.require(:m_holiday).permit(...)
end

Then you can manually set up your open_schedule string, as you've already done.

Upvotes: 1

Related Questions