In my calendar application the date is stored as a text_field
= text_field :task_time, :day, :value => display_date(@date), :id => "date-n"
I read this date in the controller’s index method but then am pre-generating default time as today in the model:
private
def generate_task_time
self.task_time = Time.now if self.task_time.nil?
end
I need to change it and set default task_time to the time currently displayed in the calendar, but am getting undefined local variable or method `params’ error when trying to read it in the model: convert_to_string(params[:task_time]
Is it possible to do it in the model at all or is it better to remove generate_task_time and do everything in the controller?
Any suggestion on the syntax –
DateTime.strptime("#{params[:date]}")
or
convert_to_string(params[:task_time]) unless params[:task_time].nil?
paramsis an instance method on the controller, so you won’t have access to it in the model. You can pass the value ofparams[:task_time]to the model, though:It’s perfectly valid to set attributes on the model from the controller. This is a common Rails pattern for updating the model attributes in a controller action:
But if you start doing too much in the controller, you get tied up with the request lifecycle and have a hard time testing your actual business logic. It’s always about balance!