Rails, how do you submit a form with a text link?

后端 未结 4 699
一整个雨季
一整个雨季 2021-02-09 14:49

I am trying to get this form to submit correctly. Here\'s what I have so far:

<% form_for(:user, :url => update_user_setting_path, :remote => true, :htm         


        
相关标签:
4条回答
  • 2021-02-09 15:22

    No, you are not using link_to properly. You need to use a submit tag to submit your form, not a link_to tag, for example:

    <% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
      ...
      <li><%= f.submit "Save" %></li>
    

    If you want to use a text link you'll have to have javascript submit the form. For example, if you are using jQuery you could do the following:

    <%= link_to 'Save', "#", :onclick=>"$('.search_form').submit()" %>
    
    0 讨论(0)
  • 2021-02-09 15:26

    I like Pan's solution but I prefer to use the ID of the form directly which you can get from the dom_id(obj). The form_for helper also uses dom_id(obj) to assign the form's ID. This way you aren't dependent on setting classes by hand or subject to accidentally submitting more than one form that share the same CSS class. It looks a little stranger but I usually have a custom FormBuilder anyway so I just add a generic link_to_submit method to encapsulate this:

    <%= link_to 'Save', "#", :onclick => "$('##{dom_id(@user)}').submit()" %>
    
    0 讨论(0)
  • 2021-02-09 15:31

    You don't need to use an id or a selector if you have jquery, you can simply do :

    = link_to 'Save', "#", onclick: "$(this).closest('form').submit()"
    
    0 讨论(0)
  • 2021-02-09 15:31

    Thanks for the answers... I ended up using this and it works great:

                  <li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), "index_users", :onclick=>"document.forms['form1'].submit();"%></li>
    
    0 讨论(0)
提交回复
热议问题