spl
spl

Reputation: 609

Rails - Form_For Submit using a Link and not a Button

For my rails application, I am using a form_for to submit form and was wondering how I would adjust it so that it is using a link and not a button to submit just this form alone and not the other forms in my application. I have referenced the posting here (Rails 3 submit form with link) but where do I put the link_to companion link?

companion link: link_to 'submit me', '#', :class => 'submit_me'

_view.html.erb

<%= form_for(current_user.likes.build(blog_id: blog.id)) do |f| %>
     <div><%= f.hidden_field :blog_id %></div>
     <%= f.submit "SUBMIT" %>
<% end %>

.js

$('.submit_me').click(function() {
  $('form').submit();
  return false;
});

Upvotes: 2

Views: 1704

Answers (1)

professormeowingtons
professormeowingtons

Reputation: 3514

Plop this in your application helper

def link_to_submit(*args, &block)
  link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end

And then you can generate links in your views as follows:

<%= link_to_submit 'Submit Form' %>

Upvotes: 1

Related Questions