Smashing
(Smashing)
1
For some reason The symbols in my new timesheet form aren’t being recognized after I nested the routes.
I go to: http://localhost:3000/users/1/timesheets/new
and receive the error:
NoMethodError in Timesheets#new
Showing /Rails/timecard/app/views/timesheets/new.html.erb where line #8 raised:
undefined method `user_id' for #<User:0x00000001eb0748>
Extracted source (around line #8):
5: 6: <div class="field">
7: <%= f.label :user_id %><br />
8: <%= f.text_field :user_id %>
9: </div>
10: <div class="field">
11: <%= f.label :time_in %><br />
I have 2 models, Timesheet and User
Timesheet belongs_to :user
User has_many :timesheets
routes.rb:
Perhaps I just have never seen your kind of defining form_for. But I had expected something like:
<%= form_for(@timesheet, :url => user_timesheets_path(@timesheet.user)) do |f| %>
<%= f.label :user_id %>
<%= f.text_field :user_id %>
<%= f.label :time_in %>
<%= f.datetime_select :time_in %>
<%= f.label :time_out %>
<%= f.datetime_select :time_out %>
<%= f.label :comments %>
<%= f.text_area :comments %>
<%= f.submit %>
Your NoMethodError tells us too: it has got a User, but needs a Timesheet…
Sorry, have found your problem:
<%= form_for( [@timesheet.user, @timesheet] ) do |f| %>
It has to be an Array...
Smashing
(Smashing)
4
<%= form_for( [@timesheet.user, @timesheet] ) do |f| %>
Thank you, you’ve been a HUGE help, just missed where it said it needed to be an array. I’ll be more thorough next time I’m reading the api docs 