Submit a form using a link

I have problem submiting a form follow by redirecting the user using a link. I am currently using form_for. Is it the right way? Or I should use form_remote_tag? Can anyone please help me?

fries 88 wrote:

I have problem submiting a form follow by redirecting the user using a link. I am currently using form_for. Is it the right way? Or I should use form_remote_tag? Can anyone please help me?

I'm not sure what you mean.

Are you trying to submit a form using a link (instead of a button)?

form_for will create a form that posts to some action, then renders whatever that action decides to do (which may be a redirect).

form_remote_tag submits the form asynchronously -- this is for Ajax -- and can render some content on completion, but you shouldn't redirect after an Ajax response.

Why do you want to use a link? It’s really not such a good idea, as people are generally used to using a button.

If it’s an absolute requirement, the approach I have always taken is to apply CSS to the button to make it look like a link.

Other solutions involve making the link call a javascript function that submits the form. Yuk.

Brian Hogan wrote:

Why do you want to use a link? It's really not such a good idea, as people are generally used to using a button.

If it's an absolute requirement, the approach I have always taken is to apply CSS to the button to make it look like a link.

Other solutions involve making the link call a javascript function that submits the form. Yuk.

<%= link_to_function 'Submit', "$('form_id').submit()" %>

But yeah, yuk. Stick with a button unless there's a good reason for a link (design-wise).

Jeremy Weiskotten wrote:

Brian Hogan wrote:

Why do you want to use a link? It's really not such a good idea, as people are generally used to using a button.

If it's an absolute requirement, the approach I have always taken is to apply CSS to the button to make it look like a link.

Other solutions involve making the link call a javascript function that submits the form. Yuk.

<%= link_to_function 'Submit', "$('form_id').submit()" %>

But yeah, yuk. Stick with a button unless there's a good reason for a link (design-wise).

Hmm...So which form should i use? I am supposed to save the various fields and go to the next page when i click the link which i am going to name next. I have tried to give the form_for an id by doing this:

<% form_for :e0_1_prod_desc,:id => 'prod', :url => { :action => :save_prod_desc } do |n| %>

Is it the right way? And i just saw from somewhere, <%= link_to_function 'Submit', "$('form_id').submit(0)" %>

Why is submit(0)? What is the difference between submit(0) and submit()?

Thanks

Ruby is not FORTRAN! It does not have limits on the names of its objects, fields or anything! So why are you limiting description down to “desc” and what I can only assume to be product down to “prod”. This is not readable.

Why are you using a link to submit the form? Use remote_form_for:

http://noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actionview/helpers/prototypehelper/remote_form_for.html

Ryan Bigg wrote:

Ruby is not FORTRAN! It does not have limits on the names of its objects, fields or anything! So why are you limiting description down to "desc" and what I can only assume to be product down to "prod". This is not readable.

Why are you using a link to submit the form? Use remote_form_for:

http://noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actionview/helpers/prototypehelper/remote_form_for.html

-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email.

Hi,

I might not be clear in the previous postings. From what I understand a form can only have one submit button.Thats why I want to use link. What I want to do is to have two submit links in a form(named previous and next respectively). So no matter which link I click, I will save the form and get redirected to another page. Which shows and other form which does the same saving action.

As I am a beginner, I might be a bit slow in understanding remote_form_for. Therefore if remote_form_for is a better option, please explain.

Thanks, fries88

Hah! I get it now.

No, a form can have as many submit buttons as you want.

<%=submit_tag “Previous” %> <%=submit_tag “Next” %>

These tags generate

When your user clicks the button, only that value is sent to the server.

In the controller, you can do this:

if params[:commit] == “Previous”

stuff goes here

elsif params[:commit] == “Next”

stuff goes here.

end

Does that help?

Ooo Brian’s so close he can SMELL IT.

What he said only applies to standard forms, not ajax forms.

If you’re using remote_form_for and you have multiple submit_tags, because of remote_form_for works, it’ll only ever submit using the first submit tag you’ve put in. I’ve posted a ticket on dev.rubyonrails.org about this a while back, and I haven’t done anything about it since: http://dev.rubyonrails.org/ticket/10026

Brian Hogan wrote:

Hah! I get it now.

No, a form can have as many submit buttons as you want.

<%=submit_tag "Previous" %> <%=submit_tag "Next" %>

These tags generate <input type="submit" name="commit" value="Previous" /> <input type="submit" name="commit" value="Next" />

When your user clicks the button, only that value is sent to the server.

In the controller, you can do this:

if params[:commit] == "Previous"   # stuff goes here elsif params[:commit] == "Next"   # stuff goes here. end

Does that help?

O I see. Yup it is much clearer now. But just to make sure, this are the changes I made to my controller:

def Prod_Desc      @product = Product.new   end

def save_prod_desc    @product = Product.new(params[:product])       if @product.save && params[:commit] == "Previous"         redirect_to :action => 'Previous_Page'       elsif @product.save && params[:commit] == "Next"         redirect_to => 'Next_Page'       else         render :action=> 'Prod_Desc'     end   end

And I am still using form_for and I just added <p><%=submit_tag "Next" %></p> and <p><%=submit_tag "Previous" %></p>

Is it correct?

Close… Try this:

def save_prod_desc @product = Product.new(params[:product]) if @product.save

  if params[:commit] == "Previous"
    redirect_to :action => 'previous_page'

  elsif params[:commit] == "Next"
    redirect_to :action=> 'next_page'
 end

else
    render :action=> 'prod_eesc'
end

end

Also… refactor your actions. Controller actions should not start with upper-case letters. They are methods. Anything starting with an uppercase letter is a Constant in Ruby. I lower-cased them in the above code.

Ahh but he’s not trying to use ajax forms. He wants to use standard forms. I believe you suggested he do it with AJAX which is completely unnecessary in this instance.

Think of the HTML. They are completely different types: Button => <input>... Link => <a href="example.com/....">

Button => POST (or GET) Link => GET

It's just how they decided to implement HTML. We've been dealing with this since HTML was introduced.

Links should also be idempotent (adjective: denoting an element of a set that is unchanged in value when multiplied or otherwise operated on by itself.). GET requests should have no effect other than returning the information requested. Links use HTTP GET requests and should be safe to be reused over-and-over without adverse side- effects.

In order to force a link do a POST, well that takes some JavaScript. Users generally don't expect this behavior from a web application.

Hope that helps explain things a bit.

Anybody remember Google's Web Accelerator? (http:// webaccelerator.google.com/)

Then this article appeared "Googles Accelerator Breaks Web Apps, Security"

"Web Accelerators problems appear to extend beyond forum sites, though. Web-based software developer 37Signals LLC began blocking the program after discovering that it was initiating links which performed critical functions, such as account deletions, on 37Signals Web applications."

Jason Fried's article http://www.37signals.com/svn/archives2/google_web_accelerator_hey_not_so_fast.php

That's why I only use links to link to another page/file. But maybe Google has "fixed" it.

my 2 cents

John