Solved

link button to submit form

Posted on 2008-10-10
6
3,709 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rails 3 if Check View? 3 292
pervasive sql odbc link to ruby on rails 12 663
What does << do and mean in Ruby 2 474
What is File vs !File in Ruby 3 379
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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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