I've had what I think is the same issue. The need to associate something with something that hasn't yet been saved. It's only an issue when doing AJAX stuff on the 'new' page. Saving it automatically on creation and then redirecting to the edit screen is a straight-forward way of dealing with it and keeps things simple. It has the downside though of creating a record whenever the 'new' action is invoked for that class. This goes against the rule that HTTP Get requests shouldn't have side-effects. For example, a search-engine indexing your site would now cause a record to be created just by accessing the 'new' page.
The other approach I've had some success with is to create the new record (if it doesn't exist) whenever the first AJAX action occurs that requires it. I store the id of the created object in the session so that it can be readily located by the next AJAX action or the by 'create' controller action. So I create a helper method in the controller something like: def locate_thing thing_id = session[:current_thing] thing = Thing.find_or_create(thing_id) thing_id || session[:current_thing] = thing.id end It's more complicated but means fewer extraneous records are being created. /Ritchie On Thu, Sep 17, 2009 at 4:42 AM, blueHandTalking <[email protected]> wrote: > > So for completion, I discovered the source of my error. > > I was creating a 'new' page from the projects controller, where I > had not saved the @photo instance which was also created. > The @photo belongs_to the project. I thought I could use find > to locate the instance, but this is now clearly not the way things > work. Need to save before something can be found. > > I have gone back to my original concept of creating instances in > project controller, saving them and then redirect_to 'edit'. In edit > I create new instance @project and @photo. > > Now my method 'upload_main' works correctly if i use: > > @photo = Photo.find_by_project_id(params[:id]) > > Thanks for your help Matt. > > I have yet another question, which will be a new post regarding > @photo.save > > Jet > > On Sep 15, 5:34 pm, Matt Jones <[email protected]> wrote: > > On Sep 15, 2009, at 7:48 PM, blueHandTalking wrote: > > > > > > > > > I would like to use a flash notice to check the params hash. > > > > > Is this the correct way to construct the assignment and call in the > > > controller: > > > > > flash[:notice]= @photo.params.inspect > > > > Either use @photo.inspect or @photo.attributes.inspect - both should > > work. > > > > --Matt Jones > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Hobo Users" 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/hobousers?hl=en -~----------~----~----~----~------~----~------~--~---
