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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. 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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to