Solved

link button to submit form

Posted on 2008-10-10
6
3,710 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ruby on Rails Installation Error 5 316
Replace text in files 3 482
How to get POST to show a page on ruby and rails 5 331
Can't get video.js working with firefox 6 900
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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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