I have two forms that people need to fill out, an Incident form and a Timesheet form. I want the Timesheet form to appear right after a user submits the Incident form. This is my code for the Incident controller; --------------------------------------------------- if @incident.save format.html { redirect_to new_timesheet_path(@incident.id) } --------------------------------------------------- After clicking submit, that takes me to this URL (a blank page): http://localhost:3000/timesheets/new.16 What do I put in the Timesheets controller to make the new Timesheet form appear, and have it connected to the Incident that was just submitted? (Both Incident and Timesheet are top level resources.) Thanks! -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968107:> I have two forms that people need to fill out, an Incident form and a > Timesheet form. I want the Timesheet form to appear right after a user > submits the Incident form. > > This is my code for the Incident controller; > --------------------------------------------------- > if @incident.save > format.html { redirect_to new_timesheet_path(@incident.id) } > ---------------------------------------------------Bizarre. new_*_path doesn''t normally take an argument (except for an optional format string, which is why you''re getting the .16 below).> > After clicking submit, that takes me to this URL (a blank page): > http://localhost:3000/timesheets/new.16What does your routes file look like, or the output from rake routes?> > What do I put in the Timesheets controller to make the new Timesheet > form appear, and have it connected to the Incident that was just > submitted?Do you have any associations set up between the Timesheet and Incident models?> > (Both Incident and Timesheet are top level resources.)Perhaps they shouldn''t be in this case.> > Thanks!Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
Marnen Laibow-Koser wrote in post #968112:> Bizarre. new_*_path doesn''t normally take an argument (except for an > optional format string, which is why you''re getting the .16 below).Is there another way to pass the incident object to the New Timesheet method? The new Timesheet needs to know it''s supposed to be attached to the Incident.> What does your routes file look like, or the output from rake routes?resources :incidents resources :timesheets> Do you have any associations set up between the Timesheet and Incident > models?The Incident model has_one :timesheet> (Both Incident and Timesheet are top level resources.) > Perhaps they shouldn''t be in this case.How can I nest them? -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968117:> Marnen Laibow-Koser wrote in post #968112: >> Bizarre. new_*_path doesn''t normally take an argument (except for an >> optional format string, which is why you''re getting the .16 below). > > Is there another way to pass the incident object to the New Timesheet > method?new_timesheet_path goes to TimesheetsController#new , not Timesheet.new . The new Timesheet needs to know it''s supposed to be attached to> the Incident. > >> What does your routes file look like, or the output from rake routes? > > resources :incidents > resources :timesheets > >> Do you have any associations set up between the Timesheet and Incident >> models? > > The Incident model has_one :timesheetAnd Timesheet belongs_to :incident , I hope?> >> (Both Incident and Timesheet are top level resources.) >> Perhaps they shouldn''t be in this case. > > How can I nest them?See the routing documentation. Once you *do* nest them, you get the routes you need for free. If you don''t want to nest them, other solutions are possible, but I''d advise nesting if it''s feasible for your project. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
Thank you, I appreciate your help so far, I feel like we''re getting there!> And Timesheet belongs_to :incident , I hope?Correct.> See the routing documentation. Once you *do* nest them, you get the > routes you need for free. > > If you don''t want to nest them, other solutions are possible, but I''d > advise nesting if it''s feasible for your project.Ok, I have nested them like this: ---------------------------- resources :incidents do resources :timesheets end ---------------------------- And I changed Incidents#create to this: ---------------------------- if @incident.save format.html { redirect_to incident_timesheet_url(@incident) } ---------------------------- Is that the ''free route'' you mentioned? On a side note, by changing the resources to nested instead of top level, it broke my Index page which shows a list of Incidents and their Timesheets. It pointed out this line of code: ---------------------------- <td><%= link_to ''View Time Sheet'', timesheet_path(incident) %></td> ---------------------------- -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968122:> Thank you, I appreciate your help so far, I''ve been struggling with this > for days now. > >> And Timesheet belongs_to :incident , I hope? > > Correct. > >> See the routing documentation. Once you *do* nest them, you get the >> routes you need for free. >> >> If you don''t want to nest them, other solutions are possible, but I''d >> advise nesting if it''s feasible for your project. > > Ok, I have nested them like this: > ---------------------------- > resources :incidents do > resources :timesheets > end > ---------------------------- > > And I changed Incidents#create to this: > ---------------------------- > if @incident.save > format.html { redirect_to incident_timesheet_url(@incident) } > ---------------------------- > > Is that the ''free route'' you mentioned?It''s one of them, yes.> > On a side note, by changing the resources to nested instead of top > level, it broke my Index page which shows a list of Incidents and their > Timesheets. It pointed out this line of code: > > ---------------------------- > <td><%= link_to ''View Time Sheet'', timesheet_path(incident) %></td> > ----------------------------Right, of course. Run rake routes to see what your routes look like now. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968122:> Thank you, I appreciate your help so far, I''ve been struggling with this > for days now. > >> And Timesheet belongs_to :incident , I hope? > > Correct. > >> See the routing documentation. Once you *do* nest them, you get the >> routes you need for free. >> >> If you don''t want to nest them, other solutions are possible, but I''d >> advise nesting if it''s feasible for your project. > > Ok, I have nested them like this: > ---------------------------- > resources :incidents do > resources :timesheets > end > ----------------------------Something else I just realized. Since Incident :has_one Timesheet, you probably want a singular resource -- resource :timesheet instead of resources :timesheets . Again, please check the docs rather than taking my word for it. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
>> On a side note, by changing the resources to nested instead of top >> level, it broke my Index page which shows a list of Incidents and their >> Timesheets. It pointed out this line of code: >> >> ---------------------------- >> <td><%= link_to ''View Time Sheet'', timesheet_path(incident) %></td> >> ---------------------------- > > Right, of course. Run rake routes to see what your routes look like > now.Ok, I ran ''rake routes'' and it showed: -------------------------------- incident_timesheet POST /incidents/:incident_id/timesheet(.:format) { :action=>"create", :controller=>"timesheets"} ---------------------------------- So I changed the code in my Index view to: --------------------------------- <td><%= link_to ''View Time Sheet'', incident_timesheet %></td> --------------------------------- That triggers: undefined local variable or method `incident_timesheet'' -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968131:>>> On a side note, by changing the resources to nested instead of top >>> level, it broke my Index page which shows a list of Incidents and their >>> Timesheets. It pointed out this line of code: >>> >>> ---------------------------- >>> <td><%= link_to ''View Time Sheet'', timesheet_path(incident) %></td> >>> ---------------------------- >> >> Right, of course. Run rake routes to see what your routes look like >> now. > > Ok, I ran ''rake routes'' and it showed: > -------------------------------- > incident_timesheet GET /incidents/:incident_id/timesheet(.:format) > {:action=>"show", :controller=>"timesheets"} > ---------------------------------- > > So I changed the code in my Index view to: > --------------------------------- > <td><%= link_to ''View Time Sheet'', incident_timesheet %></td> > --------------------------------- > > That triggers: undefined local variable or method `incident_timesheet''Of course. You forgot the _path. Please go read the routing docs again, paying particular attention to the autogenerated helpers. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 13, 2010, at 3:08 PM, Finne Jager wrote:>>> On a side note, by changing the resources to nested instead of top >>> level, it broke my Index page which shows a list of Incidents and >>> their >>> Timesheets. It pointed out this line of code: >>> >>> ---------------------------- >>> <td><%= link_to ''View Time Sheet'', timesheet_path(incident) %></td> >>> ---------------------------- >> >> Right, of course. Run rake routes to see what your routes look like >> now. > > Ok, I ran ''rake routes'' and it showed: > -------------------------------- > incident_timesheet POST /incidents/:incident_id/timesheet(.:format) > { :action=>"create", :controller=>"timesheets"} > ---------------------------------- > > So I changed the code in my Index view to: > --------------------------------- > <td><%= link_to ''View Time Sheet'', incident_timesheet %></td> > --------------------------------- > > That triggers: undefined local variable or method `incident_timesheet'' > > --OK, you''re really close. However, think about that route. It needs an :incident_id and you haven''t given one. For the create, you''ll want to do incident_timesheet_path(incident) -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- 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.
> OK, you''re really close. However, think about that route. It needs > an :incident_id and you haven''t given one. For the create, you''ll > want to do incident_timesheet_path(incident)Thank you, I realized that as well and put the (incident) parameter: <td><%= link_to ''View Time Sheet'', incident_timesheet_path(incident) %></td> When I click on the View Timesheet link in the Index, I get: ---- undefined method `edit_timesheet_path'' ---- Which has to do with this line in timesheets/show.html.erb: <%= link_to ''Edit'', edit_timesheet_path %> Am I referencing this edit path correctly? As per your advice I will go ahead and read the whole Routing documentation from front to back again because I think this will keep coming up as I adjust the rest of the code I already had. -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968154:>> OK, you''re really close. However, think about that route. It needs >> an :incident_id and you haven''t given one. For the create, you''ll >> want to do incident_timesheet_path(incident) > > Thank you, I realized that as well and put the (incident) parameter: > <td><%= link_to ''View Time Sheet'', incident_timesheet_path(incident) > %></td> > > When I click on the View Timesheet link in the Index, I get: > ---- > undefined method `edit_timesheet_path'' > ---- > > Which has to do with this line in timesheets/show.html.erb: > <%= link_to ''Edit'', edit_timesheet_path %> > > Am I referencing this edit path correctly?Instead of asking us, look at your rake routes output again! That''s why it''s there.> > As per your advice I will go ahead and read the whole Routing > documentation from front to back again because I think this will keep > coming up as I adjust the rest of the code I already had.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
> Instead of asking us, look at your rake routes output again! That''s why > it''s there.Thanks, I just did and realized what I had to change. With these nested resources in place, do I still have to set @timesheet in TimesheetsController#show? This is what I had before: ------------------------- def show @timesheet = current_user.timesheets.find(params[:id]) ------------------------- The reason I''m asking is because in timehsheets/show.html.erb I have: ------------------------- <p> <b>Date:</b> <%= timesheet.date %> </p> ------------------------- which is not working (undefined local variable or method `timesheet''). I just want to retrieve the value for ''date'' in the database timesheet table.. -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968340:>> Instead of asking us, look at your rake routes output again! That''s why >> it''s there. > > Thanks, I just did and realized what I had to change. > > With these nested resources in place, do I still have to set @timesheet > in TimesheetsController#show?Of course. Changing routes.rb only changes the routing, nothing else. You may want to use a plugin such as make_resourceful to abstract some of this.> > This is what I had before: > ------------------------- > def show > @timesheet = current_user.timesheets.find(params[:id]) > -------------------------Why the current_user part? Just Timesheet.find(params[:id]) should do the trick -- after all, the ID is unique.> > The reason I''m asking is because in timehsheets/show.html.erb I have: > ------------------------- > <p> > <b>Date:</b> > <%= timesheet.date %> > </p> > ------------------------- > which is not working (undefined local variable or method `timesheet''). I > just want to retrieve the value for ''date'' in the database timesheet > table..Of course that''s not working. You haven''t defined timesheet anywhere. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
> Why the current_user part? Just Timesheet.find(params[:id]) should do > the trick -- after all, the ID is unique.I read in Beginning Rails 3 that current_user makes sure that the logged in user can not see other people''s incidents/timesheets. I have the same thing in the IncidentsController: ------------------------------ def index @incidents = current_user.incidents.all -------------------------------> Of course that''s not working. You haven''t defined timesheet anywhere.@timesheet = Timesheet.find(params[:id]) Seems to be not working... Does it even need to find by ID if I''m already using the incident_timesheet_path(incident) link? -- Posted via http://www.ruby-forum.com/. -- 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.
On 14 December 2010 17:39, Finne Jager <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> Why the current_user part? Just Timesheet.find(params[:id]) should do >> the trick -- after all, the ID is unique. > > I read in Beginning Rails 3 that current_user makes sure that the logged > in user can not see other people''s incidents/timesheets. I have the same > thing in the IncidentsController: > ------------------------------ > def index > @incidents = current_user.incidents.all > ------------------------------- > > >> Of course that''s not working. You haven''t defined timesheet anywhere. > > @timesheet = Timesheet.find(params[:id]) > > Seems to be not working... Does it even need to find by ID if I''m > already using the incident_timesheet_path(incident) link?All the link does is build a URL for you with appropriate params. In the controller action you then have to do things with the params. Have a look in log/development.log and you will see what params are being passed. Also I suggest having a look at the Rails Guide on debugging. You can then use ruby-debug to break into your code before the find call you have shown and inspect params to see what is there and work out why the find is not working. You can also experiment then calling find from the console to see what works. Colin -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Finne Jager wrote in post #968357:>> Why the current_user part? Just Timesheet.find(params[:id]) should do >> the trick -- after all, the ID is unique. > > I read in Beginning Rails 3 that current_user makes sure that the logged > in user can not see other people''s incidents/timesheets.True enough, though I''d probably use an authorization plugin for that. I have the same> thing in the IncidentsController: > ------------------------------ > def index > @incidents = current_user.incidents.all > ------------------------------- > > But now that Timesheets are nested within Incidents, I don''t have to > have current_user for that I guess. > >> Of course that''s not working. You haven''t defined timesheet anywhere. > > @timesheet = Timesheet.find(params[:id]) > > Seems to be not working...No, I''m sure it''s working fine. But you''re defining @timesheet in your controller, then calling timesheet (without the @) in the view.> Does it even need to find by ID if I''m > already using the incident_timesheet_path(incident) link?Yes. That only passes the ID. HTTP has no means of passing ActiveRecord objects around. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> All the link does is build a URL for you with appropriate params. In > the controller action you then have to do things with the params. > Have a look in log/development.log and you will see what params are > being passed. Also I suggest having a look at the Rails Guide on > debugging. You can then use ruby-debug to break into your code before > the find call you have shown and inspect params to see what is there > and work out why the find is not working. You can also experiment > then calling find from the console to see what works.I didn''t think of checking the log, very useful tip! I need to use the incident_id that''s being passed to find the timesheet. I tried: @timesheet = Incident.find(params[:id]).timesheet "Couldn''t find Incident without an ID". I checked by hardcoding it: @timesheet = Incident.find(1).timesheet That did work, so it must be params[:id] that''s not working. The log says that incident_id is being passed as a param though. Started GET "/incidents/1/timesheet" for 127.0.0.1 at 2010-12-14 12:33:48 -0500 Processing by TimesheetsController#show as HTML Parameters: {"incident_id"=>"1"} -- Posted via http://www.ruby-forum.com/. -- 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.
> No, I''m sure it''s working fine. But you''re defining @timesheet in your > controller, then calling timesheet (without the @) in the view.Got it, I changed it back to @timesheet.>> Does it even need to find by ID if I''m >> already using the incident_timesheet_path(incident) link? > > Yes. That only passes the ID. HTTP has no means of passing > ActiveRecord objects around.Ok, thanks for the development log tip! I wasn''t aware of it. I now use the incident_id to set @timesheet like this: @timesheet = Incident.find(params[:incident_id]).timesheet And it looks like it''s working! -- Posted via http://www.ruby-forum.com/. -- 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.
On 14 December 2010 18:44, Finne Jager <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> No, I''m sure it''s working fine. But you''re defining @timesheet in your >> controller, then calling timesheet (without the @) in the view. > > Got it, I changed it back to @timesheet. > >>> Does it even need to find by ID if I''m >>> already using the incident_timesheet_path(incident) link? >> >> Yes. That only passes the ID. HTTP has no means of passing >> ActiveRecord objects around. > > Ok, thanks for the development log tip! I wasn''t aware of it. > I now use the incident_id to set @timesheet like this: > @timesheet = Incident.find(params[:incident_id]).timesheet > > And it looks like it''s working!Great. Do have a go with ruby-debug too. You should not need it often but sometimes when you just cannot understand what is going on then it is invaluable. Colin -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #968413:> On 14 December 2010 18:44, Finne Jager <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Ok, thanks for the development log tip! I wasn''t aware of it. >> I now use the incident_id to set @timesheet like this: >> @timesheet = Incident.find(params[:incident_id]).timesheet >> >> And it looks like it''s working! > > Great. Do have a go with ruby-debug too. You should not need it > often but sometimes when you just cannot understand what is going on > then it is invaluable. > > ColinI definitely will! Many thanks for taking the time to help me with all this, I really appreciate it :) -- Posted via http://www.ruby-forum.com/. -- 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.
Just another quick question if you don''t mind. I''m trying to manually append a SafetyOfficer to the Timesheet with the Rails console: Incident.first.timesheet.safety_officer << SafetyOfficer.last NoMethodError: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.<< I searching for a solution but can''t find anything. Do you know what this error message means? All the fields in SafetyOfficer.last are correct, and ''timesheet_id'' is nil. -- Posted via http://www.ruby-forum.com/. -- 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.
On 12/15/10 12:40 PM, Finne Jager wrote:> Just another quick question if you don''t mind. > > I''m trying to manually append a SafetyOfficer to the Timesheet with the > Rails console: > > Incident.first.timesheet.safety_officer<< SafetyOfficer.lastThis is pulling just the first record found of incident so its an object of Incident with an associated object of timesheet with an associate object of security_officer. You need to have an array for this as you are appending an object to an array. It looks like an incident has 1 time sheet and that a timesheet should have many security officers, so that should be plural. Incident.first.timesheet.safety_officers << SafetyOfficer.last You will want to make sure that you are using a join for this and it would be best to have this action in the join table''s create action, IMHO at least. If this is not what you are looking to then maybe some more detail as to what you are trying to accomplish would help.> NoMethodError: You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.<< > > > I searching for a solution but can''t find anything. Do you know what > this error message means? All the fields in SafetyOfficer.last are > correct, and ''timesheet_id'' is nil. >-- 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.
Jesse wrote in post #968644:> On 12/15/10 12:40 PM, Finne Jager wrote: >> Just another quick question if you don''t mind. >> >> I''m trying to manually append a SafetyOfficer to the Timesheet with the >> Rails console: >> >> Incident.first.timesheet.safety_officer<< SafetyOfficer.last > This is pulling just the first record found of incident so its an object > of Incident with an associated object of timesheet with an associate > object of security_officer. You need to have an array for this as you > are appending an object to an array. > > It looks like an incident has 1 time sheet and that a timesheet should > have many security officers, so that should be plural. > > Incident.first.timesheet.safety_officers << SafetyOfficer.last > > You will want to make sure that you are using a join for this and it > would be best to have this action in the join table''s create action, > IMHO at least. > > If this is not what you are looking to then maybe some more detail as to > what you are trying to accomplish would help.The Timesheet only has_one SafetyOfficer: -------------------------- class Timesheet < ActiveRecord::Base belongs_to :incident has_one :command_officer has_one :fire_chief has_many :fire_fighters has_one :safety_officer has_many :emts ... and about 10 more --------------------------- The only thing I want to do is to append the SafetyOfficer.last to the Timesheet of Incident.first . I did this earlier today with a FireFighter and it worked fine. I don''t know why it''s not accepting the SafetyOfficer if I''m doing it the same way. -- Posted via http://www.ruby-forum.com/. -- 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.
On 12/15/10 1:19 PM, Finne Jager wrote:> Jesse wrote in post #968644: >> On 12/15/10 12:40 PM, Finne Jager wrote: >>> Just another quick question if you don''t mind. >>> >>> I''m trying to manually append a SafetyOfficer to the Timesheet with the >>> Rails console: >>> >>> Incident.first.timesheet.safety_officer<< SafetyOfficer.last >> This is pulling just the first record found of incident so its an object >> of Incident with an associated object of timesheet with an associate >> object of security_officer. You need to have an array for this as you >> are appending an object to an array. >> >> It looks like an incident has 1 time sheet and that a timesheet should >> have many security officers, so that should be plural. >> >> Incident.first.timesheet.safety_officers<< SafetyOfficer.last >> >> You will want to make sure that you are using a join for this and it >> would be best to have this action in the join table''s create action, >> IMHO at least. >> >> If this is not what you are looking to then maybe some more detail as to >> what you are trying to accomplish would help. > The Timesheet only has_one SafetyOfficer: > -------------------------- > class Timesheet< ActiveRecord::Base > belongs_to :incident > > has_one :command_officer > has_one :fire_chief > has_many :fire_fighters > has_one :safety_officer > has_many :emts > > ... and about 10 more > --------------------------- > > The only thing I want to do is to append the SafetyOfficer.last to the > Timesheet of Incident.first . > I did this earlier today with a FireFighter and it worked fine. I don''t > know why it''s not accepting the SafetyOfficer if I''m doing it the same > way. >Are your associations the same for FireFighter and SafteyOfficer? -- 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.
Jesse wrote in post #968651:> On 12/15/10 1:19 PM, Finne Jager wrote: >>> object of security_officer. You need to have an array for this as you >>> >> has_one :safety_officer >> > Are your associations the same for FireFighter and SafteyOfficer?Timesheet has_many :fire_fighters has_one :safety_officer And both FireFighter and SafetyOfficer belongs_to :timesheet The models both have timesheet_id fields as well. -- Posted via http://www.ruby-forum.com/. -- 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.
On 12/15/10 1:27 PM, Finne Jager wrote:> Jesse wrote in post #968651: >> On 12/15/10 1:19 PM, Finne Jager wrote: >>>> object of security_officer. You need to have an array for this as you >>>> >>> has_one :safety_officer >>> >> Are your associations the same for FireFighter and SafteyOfficer? > Timesheet > has_many :fire_fighters > has_one :safety_officer > > And both FireFighter and SafetyOfficer belongs_to :timesheet > The models both have timesheet_id fields as well. >Theres the difference. Incident.first.timesheet.fire_fighters is an array of firefighter objects and thus you can append a new member to it. Incident.first.timesheet.safety_officer is a direct possession of a safety_officer object, so you are not appending a safety_officer to it, but rather adding its ID to a safety_officer_id column. So array << new_array_member would not work in this case. If there i to be only 1 safety_officer or a timesheet then the way you would want to create the association is to have a form for the timesheet in question that has the value of the safety_officer_id attribute set to the id of the safety_officer you want to associate to the time sheet. Submit and let rails do the work. It should glean that you are trying to update just the 1 attribute for that timesheet and trigger an update action on it. -- 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.
Jesse wrote in post #968656:> On 12/15/10 1:27 PM, Finne Jager wrote: >> >> And both FireFighter and SafetyOfficer belongs_to :timesheet >> The models both have timesheet_id fields as well. >> > Theres the difference. > > Incident.first.timesheet.fire_fighters is an array of firefighter > objects and thus you can append a new member to it. > > Incident.first.timesheet.safety_officer is a direct possession of a > safety_officer object, so you are not appending a safety_officer to it, > but rather adding its ID to a safety_officer_id column. > > So > array << new_array_member would not work in this case. > > If there i to be only 1 safety_officer or a timesheet then the way you > would want to create the association is to have a form for the timesheet > in question that has the value of the safety_officer_id attribute set to > the id of the safety_officer you want to associate to the time sheet. > Submit and let rails do the work. It should glean that you are trying to > update just the 1 attribute for that timesheet and trigger an update > action on it.I understand what I''m doing wrong. I don''t have to append the SafetyOfficer at all, I just need to SET it because there can be only one SafetyOfficer. Incident.first.timesheet.safety_officer = SafetyOfficer.last -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968650:> Jesse wrote in post #968644: >> On 12/15/10 12:40 PM, Finne Jager wrote: >>> Just another quick question if you don''t mind.Please start a new thread for a new issue in the future. [...]> The Timesheet only has_one SafetyOfficer: > -------------------------- > class Timesheet < ActiveRecord::Base > belongs_to :incident > > has_one :command_officer > has_one :fire_chief > has_many :fire_fighters > has_one :safety_officer > has_many :emts > > ... and about 10 more > ---------------------------Can one safety officer appear on multiple timesheets? If so, then you''ll want all those has_one associations to be belongs_to instead. Please review the difference between the two. Likewise, has_many :fire_fighters should probably be has_and_belongs_to_many, for the same reason. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
Marnen Laibow-Koser wrote in post #968664:> Finne Jager wrote in post #968650: >> Jesse wrote in post #968644: >>> On 12/15/10 12:40 PM, Finne Jager wrote: >>>> Just another quick question if you don''t mind. > > Please start a new thread for a new issue in the future. > > [...] >> The Timesheet only has_one SafetyOfficer: >> -------------------------- >> class Timesheet < ActiveRecord::Base >> belongs_to :incident >> >> has_one :command_officer >> has_one :fire_chief >> has_many :fire_fighters >> has_one :safety_officer >> has_many :emts >> >> ... and about 10 more >> --------------------------- > > Can one safety officer appear on multiple timesheets? If so, then > you''ll want all those has_one associations to be belongs_to instead. > Please review the difference between the two. Likewise, has_many > :fire_fighters should probably be has_and_belongs_to_many, for the same > reason.I understand what you mean with that, the way I have it set up is that the personnel is unique for each time sheet. The FireChief model for example also has fields that contain ''time spent on scene'' and ''hourly rate'' etc. -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968666:> Marnen Laibow-Koser wrote in post #968664: >> Finne Jager wrote in post #968650: >>> Jesse wrote in post #968644: >>>> On 12/15/10 12:40 PM, Finne Jager wrote: >>>>> Just another quick question if you don''t mind. >> >> Please start a new thread for a new issue in the future. > > Ok, will do! > >> [...] >>> The Timesheet only has_one SafetyOfficer: >>> -------------------------- >>> class Timesheet < ActiveRecord::Base >>> belongs_to :incident >>> >>> has_one :command_officer >>> has_one :fire_chief >>> has_many :fire_fighters >>> has_one :safety_officer >>> has_many :emts >>> >>> ... and about 10 more >>> --------------------------- >> >> Can one safety officer appear on multiple timesheets? If so, then >> you''ll want all those has_one associations to be belongs_to instead. >> Please review the difference between the two. Likewise, has_many >> :fire_fighters should probably be has_and_belongs_to_many, for the same >> reason. > > I understand what you mean with that. The way I have it set up is that > the personnel is unique for each time sheet. The FireChief model for > example also has fields that contain ''time spent on scene'' and ''hourly > rate'' etc.Wait, so if Fire Chief Bob responds to three incidents, he''s got three FireChief records? If so, that''s very poor data modeling. What you want to do in that case is set up a join model -- call it Attendance or something -- associated both with the timesheet and the person. This would contain information unique to *that combination*, so there would be one Attendance record for Bob at today''s car accident, a second for Bob at tomorrow''s train wreck, and so on. If this is foreign to you, please go read about database normalization. The Wikipedia articles are excellent. Further thoughts: * FireChief, SafetyOfficer, and so on may not have to be separate tables or classes. * Attendance will probably do for any type of personnel, so you probably wouldn''t need separate SafetyOfficerAttendance, FireChiefAttendance, and so on. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
>> I understand what you mean with that. The way I have it set up is that >> the personnel is unique for each time sheet. The FireChief model for >> example also has fields that contain ''time spent on scene'' and ''hourly >> rate'' etc. > > Wait, so if Fire Chief Bob responds to three incidents, he''s got three > FireChief records? > > If so, that''s very poor data modeling. What you want to do in that case > is set up a join model -- call it Attendance or something -- associated > both with the timesheet and the person. This would contain information > unique to *that combination*, so there would be one Attendance record > for Bob at today''s car accident, a second for Bob at tomorrow''s train > wreck, and so on. > > If this is foreign to you, please go read about database normalization. > The Wikipedia articles are excellent. > > Further thoughts: > * FireChief, SafetyOfficer, and so on may not have to be separate tables > or classes. > * Attendance will probably do for any type of personnel, so you probably > wouldn''t need separate SafetyOfficerAttendance, FireChiefAttendance, and > so on.This system will be used by multiple Fire Departments to submit data from their incident/time sheets from the scene of an accident. If I were to use a join model like that, does that mean that a Fire Department has to initially enter all their personnel into the database, then select per incident which person responded, like a checklist? They often use ''outside'' teams as well, so if they use different people all the time, wouldn''t that checklist become very long? -- Posted via http://www.ruby-forum.com/. -- 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.
Finne Jager wrote in post #968681: [...]> This system will be used by multiple Fire Departments to submit data > from their incident/time sheets from the scene of an accident. > > If I were to use a join model like that, does that mean that a Fire > Department has to initially enter all their personnel into the database,Not necessarily. You could provide an interface to add new personnel on the fly. It wouldn''t have to be done in advance. Anyway, this is a user interface issue and needn''t have anything to do with the data modeling.> then select per incident which person responded, like a checklist? > They often use ''outside'' teams as well, so if they use different people > all the time, wouldn''t that checklist become very long?That''s a user interface issue. There are good ways of taming that sort of thing (say, by autocompleting a partially entered name), but they''re beyond the scope of the current data modeling discussion. You''re thinking about UI and implementation together. That''s usually a bad thing. You don''t want a UI that exposes irrelevant implementation details, and you don''t want an implementation that is tied to one UI. Part of the reason we decouple things is so that we can make interface and implementation completely independent. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.
> You''re thinking about UI and implementation together. That''s usually a > bad thing. You don''t want a UI that exposes irrelevant implementation > details, and you don''t want an implementation that is tied to one UI. > Part of the reason we decouple things is so that we can make interface > and implementation completely independent.I understand, it looks like I have some remodeling to do. :) I was indeed thinking too much from a UI point of view. I''m fairly new to database modeling so I will check out the resources you mentioned. -- Posted via http://www.ruby-forum.com/. -- 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.