How to redirect to 'new'

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 :slight_smile:

On Jan 19, 3:10Â pm, Jon C. [email protected] 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

Frederick C. wrote:

On Jan 19, 3:10Â pm, Jon C. [email protected] 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

Jon C. wrote:

Frederick C. wrote:

On Jan 19, 3:10Â pm, Jon C. [email protected] 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?

Jon C. wrote:

Jon C. wrote:

Frederick C. wrote:

On Jan 19, 3:10Â pm, Jon C. [email protected] 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

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].

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

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
[email protected]

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].

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

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
[email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs