Hi guys, I''d like to be able to display my ''new'' view but have an intermediate step before saving the new record using the ''create'' button. Basically, some of the data for the model is variable and dependant on a number that the user needs to select. I envisaged a button along the lines of ''go get the extra data'' that would head off to the controller which would get the data based on the number entered by the user and redirect back to the new page (which preserved the info they already entered). The user could then make sure they are happy with this new information before they hit create. So far i''ve taken the scaffolded new.html.erb file and tried to add a ''button_to'' that has an :action that contains the name of a method i added to the controller. But the method doesn''t get hit - it calls Create instead. I think its because its wrapped in the form_for the scaffold generated. I''ve managed to confuse myself and i couldn''t find a tutorial to help. I''ve described it in general terms and hopefully this makes sense. I dont have the code at work and if need be i can post back later with it. 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.
On Jan 19, 3:10 pm, Jon Cox <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> So far i''ve taken the scaffolded new.html.erb file and tried to add a > ''button_to'' that has an :action that contains the name of a method i > added to the controller. But the method doesn''t get hit - it calls > Create instead. I think its because its wrapped in the form_for the > scaffold generated. I''ve managed to confuse myself and i couldn''t find a > tutorial to help. > > I''ve described it in general terms and hopefully this makes sense. I > dont have the code at work and if need be i can post back later with it. >Assuming you are using map.resources, sounds like you need to add the action as one of the collection actions for that resource Fred> Thanks :) > -- > Posted viahttp://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.
Frederick Cheung wrote:> On Jan 19, 3:10 pm, Jon Cox <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> So far i''ve taken the scaffolded new.html.erb file and tried to add a >> ''button_to'' that has an :action that contains the name of a method i >> added to the controller. But the method doesn''t get hit - it calls >> Create instead. I think its because its wrapped in the form_for the >> scaffold generated. I''ve managed to confuse myself and i couldn''t find a >> tutorial to help. >> >> I''ve described it in general terms and hopefully this makes sense. I >> dont have the code at work and if need be i can post back later with it. >> > Assuming you are using map.resources, sounds like you need to add the > action as one of the collection actions for that resource > > FredFred you may have picked up on what i''m missed there. I''ve not ammended the routes file at all yet. being very new to rails i''ve left as much vanilla as i can while i work my way through the learning process. Right, off to read the ruby tutorial to learn how to do this. Thanks for the reply! Jon -- Posted via http://www.ruby-forum.com/. --001485f3c20affd6c0047d85fe91 Content-Type: text/plain; charset=ISO-8859-1 -- 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. --001485f3c20affd6c0047d85fe91--
Jon Cox wrote:> Frederick Cheung wrote: >> On Jan 19, 3:10 pm, Jon Cox <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> So far i''ve taken the scaffolded new.html.erb file and tried to add a >>> ''button_to'' that has an :action that contains the name of a method i >>> added to the controller. But the method doesn''t get hit - it calls >>> Create instead. I think its because its wrapped in the form_for the >>> scaffold generated. I''ve managed to confuse myself and i couldn''t find a >>> tutorial to help. >>> >>> I''ve described it in general terms and hopefully this makes sense. I >>> dont have the code at work and if need be i can post back later with it. >>> >> Assuming you are using map.resources, sounds like you need to add the >> action as one of the collection actions for that resource >> >> Fred > > Fred you may have picked up on what i''m missed there. I''ve not ammended > the routes file at all yet. being very new to rails i''ve left as much > vanilla as i can while i work my way through the learning process. > > Right, off to read the ruby tutorial to learn how to do this. Thanks for > the reply! > > JonOk i can get this working (sort of) by placing the button_to outside the form_for, but if i place the button_to inside the form_for it doesn''t matter what i set the :action to, it always heads off to the create method (checked the generated source and sure enough it turns it into a submit). Is the use of form_for in this case wrong? -- Posted via http://www.ruby-forum.com/. --00151747b52abf6949047d8745e5 Content-Type: text/plain; charset=ISO-8859-1 -- 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. --00151747b52abf6949047d8745e5--
Jon Cox wrote:> Jon Cox wrote: >> Frederick Cheung wrote: >>> On Jan 19, 3:10 pm, Jon Cox <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> So far i''ve taken the scaffolded new.html.erb file and tried to add a >>>> ''button_to'' that has an :action that contains the name of a method i >>>> added to the controller. But the method doesn''t get hit - it calls >>>> Create instead. I think its because its wrapped in the form_for the >>>> scaffold generated. I''ve managed to confuse myself and i couldn''t find a >>>> tutorial to help. >>>> >>>> I''ve described it in general terms and hopefully this makes sense. I >>>> dont have the code at work and if need be i can post back later with it. >>>> >>> Assuming you are using map.resources, sounds like you need to add the >>> action as one of the collection actions for that resource >>> >>> Fred >> >> Fred you may have picked up on what i''m missed there. I''ve not ammended >> the routes file at all yet. being very new to rails i''ve left as much >> vanilla as i can while i work my way through the learning process. >> >> Right, off to read the ruby tutorial to learn how to do this. Thanks for >> the reply! >> >> Jon > > Ok i can get this working (sort of) by placing the button_to outside the > form_for, but if i place the button_to inside the form_for it doesn''t > matter what i set the :action to, it always heads off to the create > method (checked the generated source and sure enough it turns it into a > submit). > > Is the use of form_for in this case wrong?some digging in the API and i understand now that button_to creates a simple form. this goes inside the form created by form_for and bang.... nested forms which is a big no-no. I think i''ll need to do something that feels very wrong.... have 2 versions of the submit button. In the create method i''ll have a look at which submit button is clicked and if its the one that needs to go do ''go get the extra data'' then it''ll cancel out the save and redirect back to the ''new'' page but this time with the new data included. This should work but it''s not good design because ''Create'' isn''t there for this purpose. Is there any other way i can do this i.e. have a button inside a form that does something other than submit. Thanks in advance. Jon -- Posted via http://www.ruby-forum.com/. --0015175cd1629101e0047d9ac191 Content-Type: text/plain; charset=ISO-8859-1 -- 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. --0015175cd1629101e0047d9ac191--
On Jan 20, 2010, at 11:05 AM, Jon Cox wrote:> Jon Cox wrote: >> Jon Cox wrote: >>> Frederick Cheung wrote: >>>> On Jan 19, 3:10 pm, Jon Cox <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>>> So far i''ve taken the scaffolded new.html.erb file and tried to >>>>> add a >>>>> ''button_to'' that has an :action that contains the name of a >>>>> method i >>>>> added to the controller. But the method doesn''t get hit - it calls >>>>> Create instead. I think its because its wrapped in the form_for >>>>> the >>>>> scaffold generated. I''ve managed to confuse myself and i >>>>> couldn''t find a >>>>> tutorial to help. >>>>> >>>>> I''ve described it in general terms and hopefully this makes >>>>> sense. I >>>>> dont have the code at work and if need be i can post back later >>>>> with it. >>>>> >>>> Assuming you are using map.resources, sounds like you need to add >>>> the >>>> action as one of the collection actions for that resource >>>> >>>> Fred >>> >>> Fred you may have picked up on what i''m missed there. I''ve not >>> ammended >>> the routes file at all yet. being very new to rails i''ve left as >>> much >>> vanilla as i can while i work my way through the learning process. >>> >>> Right, off to read the ruby tutorial to learn how to do this. >>> Thanks for >>> the reply! >>> >>> Jon >> >> Ok i can get this working (sort of) by placing the button_to >> outside the >> form_for, but if i place the button_to inside the form_for it doesn''t >> matter what i set the :action to, it always heads off to the create >> method (checked the generated source and sure enough it turns it >> into a >> submit). >> >> Is the use of form_for in this case wrong? > > some digging in the API and i understand now that button_to creates a > simple form. this goes inside the form created by form_for and > bang.... > nested forms which is a big no-no. > > I think i''ll need to do something that feels very wrong.... have 2 > versions of the submit button. In the create method i''ll have a look > at > which submit button is clicked and if its the one that needs to go do > ''go get the extra data'' then it''ll cancel out the save and redirect > back > to the ''new'' page but this time with the new data included. > > This should work but it''s not good design because ''Create'' isn''t there > for this purpose. > > Is there any other way i can do this i.e. have a button inside a form > that does something other than submit. > > Thanks in advance. > > JonThere are a couple of ways to do this. They are quite similar and rely on the text of the submit control coming through in params[:commit]. 1) In a view: <% form_for(:screen, :url => screens_path(@pim)) do |f| %> <%= render :partial => ''form'', :object => f -%> <p> <%= submit_tag "Create" %> <%= submit_tag "Create and Add Question" %> </p> <% end %> And in the controller: def create @screen = Screen.new(params[:screen]) respond_to do |format| if @screen.save flash[:notice] = ''Screen was successfully created.'' format.html { redirect_to(params[:commit] =~ /add question/ i ? new_question_path(@screen) : pim_url(@pim)) } format.xml { head :created, :location => screen_url(@pim, @screen) } else format.html { render :action => "new" } format.xml { render :xml => @screen.errors.to_xml } end end end 2) In the view: <%= submit_tag "Test Pending Invoices", :id => ''test_pending_btn'' %> <%= submit_tag "Create Pending Invoices", :id => ''create_pending_btn'', :disabled => true %> This view needed to set the id because it dynamically enables the "Create..." button based on other fields on the form. And the controller: dry_run = params[''commit''] =~ /^Test/ later code does the prep and error check and skips the actual execute step if this is true. You can also give a :name option to override the ''commit'' default (which I think I''ve done before, too, but can''t think of where the example might be). -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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.
Rob Biedenharn wrote:> On Jan 20, 2010, at 11:05 AM, Jon Cox wrote: >>>>>> the >>>>> the >>>> Right, off to read the ruby tutorial to learn how to do this. >>> into a >> versions of the submit button. In the create method i''ll have a look >> that does something other than submit. >> >> Thanks in advance. >> >> Jon > > There are a couple of ways to do this. They are quite similar and rely > on the text of the submit control coming through in params[:commit]. > > 1) > > In a view: > <% form_for(:screen, :url => screens_path(@pim)) do |f| %> > <%= render :partial => ''form'', :object => f -%> > <p> > <%= submit_tag "Create" %> > <%= submit_tag "Create and Add Question" %> > </p> > <% end %> > > And in the controller: > def create > @screen = Screen.new(params[:screen]) > > respond_to do |format| > if @screen.save > flash[:notice] = ''Screen was successfully created.'' > format.html { redirect_to(params[:commit] =~ /add question/ > i ? new_question_path(@screen) : pim_url(@pim)) } > format.xml { head :created, :location => screen_url(@pim, > @screen) } > else > format.html { render :action => "new" } > format.xml { render :xml => @screen.errors.to_xml } > end > end > end > > 2) > > In the view: > <%= submit_tag "Test Pending Invoices", :id => > ''test_pending_btn'' %> > <%= submit_tag "Create Pending Invoices", :id => > ''create_pending_btn'', :disabled => true %> > > This view needed to set the id because it dynamically enables the > "Create..." button based on other fields on the form. > > > And the controller: > dry_run = params[''commit''] =~ /^Test/ > > later code does the prep and error check and skips the actual execute > step if this is true. > > You can also give a :name option to override the ''commit'' default > (which I think I''ve done before, too, but can''t think of where the > example might be). > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgThanks rob! I went with a version of what you did in number 1 and in my create method do... if param[''commit] == "Get Random Games" # go do the random game stuff and capture in session render :action => "new" then return end It works which is my main triumph lol Jon -- 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.