Submit Rails 4 form with link instead of button

后端 未结 3 1732
感动是毒
感动是毒 2021-02-04 17:12

I see some issues similar but they seem contrived and mostly for previous versions of Rails.

What is the simplest way to submit a form with an anchor tag (link) instead

3条回答
  •  眼角桃花
    2021-02-04 17:32

    Submit

    To extend the answers provided already, HTML forms have to be submitted with a submit button.

    I'm not sure exactly what special characteristics the submit button has over a link - it essentially calls the submit action, which a link cannot (info):

    enter image description here

    --

    Link

    This means if you wish to replace a submit button with a link, you'll essentially have to mimick the submit method in your application. This can be done with JS (JQuery):

    #app/assets/javascripts/application.js
    $(document).on("click", "#your_link", function(){
       $("#form").submit();
    });
    
    #app/views/controller/your_view.html.erb
    <%= form_tag your_path, id: "form" do %>
        <%= link_to "Submit", your_path, id: "your_link" %>
    <% end %>
    

提交回复
热议问题