Symbols not recognized after nesting routes - NoMethodError in Timesheets#new

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.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 %>
<% end %> -------------------------------------------------------------------------------

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.

Perhaps I just have never seen your kind of defining form_for. But I
had expected something like:

<%= 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…

<%= 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 :slight_smile:

Sorry, have found your problem:

<%= form_for( [@timesheet.user, @timesheet] ) do |f| %>

It has to be an Array…

On 27/01/11 19:58, Smashing wrote:

Showing //Rails/timecard/app/views/timesheets/new.html.erb/ where line
9:
devise_for :users
root :to => “users#punch_in”
end

<%= f.text_area :comments %> nested route. I've tried switching up Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


best regards
Bente P.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs