Smashing
2011-Jan-27 18:58 UTC
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: <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: --------------------------------------------------------- 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: <h1>New timesheet</h1> <%= form_for(@timesheet.user, @timesheet) do |f| %> <div class="field"> <%= f.label :user_id %><br /> <%= f.text_field :user_id %> </div> <div class="field"> <%= f.label :time_in %><br /> <%= f.datetime_select :time_in %> </div> <div class="field"> <%= f.label :time_out %><br /> <%= f.datetime_select :time_out %> </div> <div class="field"> <%= f.label :comments %><br /> <%= f.text_area :comments %> </div> <div class="actions"> <%= f.submit %> </div> <% 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
narrenblut
2011-Jan-27 19:11 UTC
Re: Symbols not recognized after nesting routes - NoMethodError in Timesheets#new
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| %> > <div class="field"> > <%= f.label :user_id %><br /> > <%= f.text_field :user_id %> > </div> > <div class="field"> > <%= f.label :time_in %><br /> > <%= f.datetime_select :time_in %> > </div> > <div class="field"> > <%= f.label :time_out %><br /> > <%= f.datetime_select :time_out %> > </div> > <div class="field"> > <%= f.label :comments %><br /> > <%= f.text_area :comments %> > </div> > <div class="actions"> > <%= f.submit %> > </div>Your NoMethodError tells us too: it has got a User, but needs a Timesheet... On 27.01.2011, at 19:58, Smashing wrote:> 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: > --------------------------------------------------------- > 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: > <h1>New timesheet</h1> > > <%= form_for(@timesheet.user, @timesheet) do |f| %> > > > <div class="field"> > <%= f.label :user_id %><br /> > <%= f.text_field :user_id %> > </div> > <div class="field"> > <%= f.label :time_in %><br /> > <%= f.datetime_select :time_in %> > </div> > <div class="field"> > <%= f.label :time_out %><br /> > <%= f.datetime_select :time_out %> > </div> > <div class="field"> > <%= f.label :comments %><br /> > <%= f.text_area :comments %> > </div> > <div class="actions"> > <%= f.submit %> > </div> > <% 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. > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bente Pieck
2011-Jan-27 19:13 UTC
Re: Symbols not recognized after nesting routes - NoMethodError in Timesheets#new
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:> 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: > --------------------------------------------------------- > 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: > <h1>New timesheet</h1> > > <%= form_for(@timesheet.user, @timesheet) do |f| %> > > > <div class="field"> > <%= f.label :user_id %><br /> > <%= f.text_field :user_id %> > </div> > <div class="field"> > <%= f.label :time_in %><br /> > <%= f.datetime_select :time_in %> > </div> > <div class="field"> > <%= f.label :time_out %><br /> > <%= f.datetime_select :time_out %> > </div> > <div class="field"> > <%= f.label :comments %><br /> > <%= f.text_area :comments %> > </div> > <div class="actions"> > <%= f.submit %> > </div> > <% 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. > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.-- best regards Bente Pieck -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Smashing
2011-Jan-27 19:41 UTC
Re: Symbols not recognized after nesting routes - NoMethodError in Timesheets#new
<%= 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 :) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.