Solved

link button to submit form

Posted on 2008-10-10
6
3,713 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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