Nick Ginanto
Nick Ginanto

Reputation: 32120

submit_tag without a button or image on rails

How do I create a submit action in form not by a button (with submit_tag) and not by an image (image_submit_tag)?

I'd like the submit to be clickable text like a link

Thanks

Upvotes: 3

Views: 3731

Answers (2)

liver
liver

Reputation: 498

Here's one way that I did it:

<a><%= submit_tag "Submit", class: "fake-button" %></a>

Then, change the styling:

.fake-button {
  padding: 0;
  border: none;
  display: inline;
}

So it's still technically a button but looks very much like a link. It's a bit hacky though :)

Upvotes: 0

agmcleod
agmcleod

Reputation: 13611

You would need to do that with javascript, since links are a GET request, not a post request. So let's say you have the following code:

<%= form_for @post, :html => { :id => 'form' } do |f| %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :body %>
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <a id="submit">Submit</a>
  </div>
<% end %>

You then need to add the following to one of your javascript files. If you're using < Rails 3.1, just simply add this to your application.js. If you're using Rails 3.1, add it to one of the js files in assets/javascripts directory.

$(function() {
  $('#submit').click(function() {
    $('#form').submit();
  });
});

That said, you can pass the method option into a link_to method call, to use POST. However, just doing that without javascript will not submit any of the form data. So you would either have to do the above, or use javascript to add the data to the url like you would in a query string.

Upvotes: 5

Related Questions