# How to redirect to 'new'

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-to-new/181874>\
**Category:** Rails\
**Created:** [January 19, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874 "2010-01-19T16:10:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 19, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/1 "2010-01-19T16:10:55Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 19, 2010, 4:16pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/2 "2010-01-19T16:16:59Z")

</div>

On Jan 19, 3:10Â pm, Jon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 19, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/3 "2010-01-19T16:19:28Z")

</div>

Frederick C. wrote:

> On Jan 19, 3:10Â pm, Jon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 19, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/4 "2010-01-19T17:50:50Z")

</div>

Jon C. wrote:

> Frederick C. wrote:
> 
> > On Jan 19, 3:10Â pm, Jon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 20, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/5 "2010-01-20T17:05:31Z")

</div>

Jon C. wrote:

> Jon C. wrote:
> 
> > Frederick C. wrote:
> > 
> > > On Jan 19, 3:10Â pm, Jon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 20, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/6 "2010-01-20T17:51:02Z")

</div>

On Jan 20, 2010, at 11:05 AM, Jon C. 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 -%\>

\<%= submit\_tag "Create" %\> \<%= submit\_tag "Create and Add Question" %\>

\<% 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

1. 

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![imnotquitejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@imnotquitejack](https://www.ruby-forum.com/u/imnotquitejack)\
**Post date:** [January 21, 2010, 10:56am UTC](https://www.ruby-forum.com/t/how-to-redirect-to-new/181874/7 "2010-01-21T10:56:09Z")

</div>

Rob B. wrote:

> On Jan 20, 2010, at 11:05 AM, Jon C. 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 -%\>
> 
> \<%= submit\_tag "Create" %\> \<%= submit\_tag "Create and Add Question" %\>
> 
> \<% 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
> 
> 1. 
> 
> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks 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
