Solved

link button to submit form

Posted on 2008-10-10
6
3,715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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