1

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
camdixon
  • 852
  • 2
  • 18
  • 33
  • 1
    Where you have defined `@start_time`? It is coming as `nil`. – Pavan Aug 07 '14 at 07:45
  • @Pavan for this example let's just say it's "now". I'll edit my example to show you it's set by the user. – camdixon Aug 07 '14 at 07:48
  • I guess you're right though, my problem is not just adding the time. I added the line `@start_time = DateTime.now` in front of it just for example. It worked. Although, my page is showing it's passing values. – camdixon Aug 07 '14 at 07:54
  • Yeah! That's what i meant.There should be some value defined for `@start_time` then only this `@end_time = @start_time + 30.minutes` will execute. – Pavan Aug 07 '14 at 07:56

1 Answers1

1

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
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • Oh, LOL. Well that's fairly simple then. I thought it was going into it I suppose due to a naming convention. Thanks for the help! – camdixon Aug 07 '14 at 07:58
  • I still get the same error with this. I have updated the post. What else could I be doing wrong? – camdixon Aug 07 '14 at 08:30
  • It gives me a weird list of parameters when I debug. I don't know why it says 1i and 2i and 3i on the :start_time. Would this change the code you provided? – camdixon Aug 07 '14 at 08:38
  • 1
    That's ok, those are the subdivisions for day / month / year... start_time should still be fine. – SteveTurczyn Aug 07 '14 at 09:01
  • 1
    Can you inspect `params[:start_time]` ? `def new; p '*'*80; p params[:start_time] ; p '*'*80 ...` That should print the start time in your server, easy to spot because it'll be surrounded by rows of 80 stars. – SteveTurczyn Aug 07 '14 at 09:03
  • I've been putting these in my #create method, as it currently goes from my new section of the controller to the new view, then to the #create section of my controller is where it sends the POST params. I've tried to print the 80 stars, but it doesn't show the stars it just shows the code due to dev mode. – camdixon Aug 07 '14 at 09:20
  • 1
    Ah, wow, I'm an idiot! Of course, the form data is returned to `create` or `update`, not to `new`. – SteveTurczyn Aug 07 '14 at 09:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58861/discussion-between-camdixon-and-steveturczyn). – camdixon Aug 07 '14 at 09:23
  • I tried adding the parameters together by using an answer from this post. It gives an error of "invalid date" when I try to combine the separate elements into one DateTime object. http://stackoverflow.com/questions/5073756/where-is-the-rails-method-that-converts-data-from-datetime-select-into-a-datet – camdixon Aug 07 '14 at 09:44
  • 1
    You don't need to add the parameters together. Add the 'pry' gem to your project for development and test, then just after `def create` insert a new line `binding.pry` Run in develoment mode, and when the console stops at the pry, type `params` to see what the params are. Then type `exit` to resume the job. – SteveTurczyn Aug 07 '14 at 09:55
  • I updated what is given by the byebug debugger since I'm more familiar with that debugger. I did try using pry though. The params are listed in the OP. Since they show, why would params[:start_date] not work? – camdixon Aug 07 '14 at 10:12