Solved

link button to submit form

Posted on 2008-10-10
6
3,708 Views
Last Modified: 2013-11-13
I'm sure this is really easy - once you know rails - (or if you haven't been living in a .NET sandbox for 5 years), but, how can I get a regular ole form submit button in the shape of a link?

I tried taking  <%= link_to url  "Click Me",   { :action => "foo"} %>
and turning it into
<%= link_to url  "Click Me", :method => :post,   { :action => "foo"} %>

but that created some weird error that I don't have right this second.  I know you can also set up a regular <a> tag, and point it to a javascript function that submits the form, but I know there's got to be a Rails way to do this.

Thanks!
0
Comment
Question by:ARACK04
  • 3
  • 3
6 Comments
 
LVL 1

Accepted Solution

by:
ozataman earned 500 total points
ID: 22688028
Hey there,

You should do:

<%= link_to  "Click Me",   { :action => "foo"}, {:method => :post} %>

Cheers,
OA

0
 

Author Comment

by:ARACK04
ID: 22688050
Geez.  Could I trouble you for some education on why you need the {} around the :method => :post
???

Thanks!
0
 

Author Closing Comment

by:ARACK04
ID: 31505035
THANK YOU!!!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:ozataman
ID: 22688261
Sure,

The method takes two hash arguments. Ruby will automatically convert what it sees last in the call to hash, if it is in a hash form. However, if you are going to supply two separate hashes: one has the url description and a second as html_parameters, then you would have to use the proper Hash class form { }.

Hope this helps,
OA
0
 

Author Comment

by:ARACK04
ID: 22688555
I'm completely new to rails - so would you mind explaining why the following works (from Rails API).  Why does this example not need the {}  - I don't care about adding the {} - I just want to understand better what I'm doing.  Thanks!


  link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete


  # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
0
 
LVL 1

Expert Comment

by:ozataman
ID: 22688583
I see.

In this case, @image takes care of the url destination, and all the trailing parameters then get perceived as one hash, in which case you don't have to explicitly put {}. In you original case, though, there were two separate hash arguments, and so you need to explicitly state them.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now