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:
7: <%= f.label :user_id %>
8: <%= f.text_field :user_id %>
9:
10:
11: <%= f.label :time_in %>
I have 2 models, Timesheet and User
Timesheet belongs_to :user
User has_many :timesheets
routes.rb:
Timecard::Application.routes.draw do
devise_for :users
resources :users do
resources :timesheets
collection do
get ‘punch_in’
get ‘punch_out’
post ‘punch_in_now’
post ‘punch_out_now’
end
end
root :to => “users#punch_in”
end
timesheets_controller.rb new function:
def new
@timesheet = Timesheet.new(:user_id => params[:user_id])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @timesheet }
end
end
new.html.erb - new timesheets view:
New timesheet
<%= form_for(@timesheet.user, @timesheet) do |f| %>
<%= f.text_field :user_id %>
<%= f.datetime_select :time_in %>
<%= f.datetime_select :time_out %>
<%= f.text_area :comments %>
if I remove <%= f.text_field :user_id %> then it gives the same error on
<%=
f.datetime_select :time_in %>
The view worked fine before I changed the routing to the more restful,
nested route. I’ve tried switching up
the form_for statement to diffrent things like <%= form_for(@user,
@timesheet) do |f| %> and
<%= form_for(@timesheet.user, @timesheet.build) do |f| %> with no
success,
I just dont’ understand
why changing the route would break rails understanding of what :user_id
is.
Any help would be much appreciated.