Reputation: 892
I have a variable called @start_time
and @end_time
that is a DateTime object in Rails:
@start_time = params[:start_time]
@end_time = @start_time + 30.minutes
In my controller I get the error:
undefined method '+' for nil:NilClass
I read "Time manipulation in ruby" that said I could add 30.minutes
to a DateTime class object.
My @start_time
is set by the user in a view called "new". I also confirmed that @start_time
has the correct values and is passing them into the controller.
My problem is adding 30 minutes to it.
<div class="form-group">
<%= f.label 'Lesson Start' %>
<%= f.datetime_select :start_time, class: "form-control", ampm: true %>
</div>
When I debug the incoming POST information it gives me this:
(byebug) params
{"utf8"=>"✓", "authenticity_token"=>"3ZUkfyga9SxuZqSWo8RWAIcRPHvYisQUxapgXt4Zx8E=", "lesson"=>{"user_id"=>"1", "student_id"=>"1", "start_time(1i)"=>"2014", "start_time(2i
)"=>"8", "start_time(3i)"=>"7", "start_time(4i)"=>"10", "start_time(5i)"=>"10"}, "lesson_length"=>"45", "commit"=>"Schedule Lesson", "action"=>"create", "controller"=>"le
ssons"}
I tried my parameters in my controller like this:
def create
@lesson = Lesson.new(lesson_params)
@current_date = params[:date] || Date.today
@start_time = params[:start_time]
@end_time = @start_time + 30.minutes
end
def lesson_params
params.require(:lesson).permit!
end
Upvotes: 1
Views: 2705
Reputation: 36880
You are not assigning the form data to an instance variable.
In your new method you need to do that explicitly
def create
@start_time = params[:start_time]
...
end
Upvotes: 1