Rails: submit (via AJAX) when drop-down option clicked

后端 未结 5 2064
礼貌的吻别
礼貌的吻别 2021-02-04 10:14

What is the easiest and most graceful way to auto-submit an AJAX form when a drop-down box\'s option is selected? I\'m creating an administration page where the admin can modify

5条回答
  •  情书的邮戳
    2021-02-04 11:04

    There are a couple ways to handle this. Certainly the observe field approach is one, but you could also use a simple javascript or a remote_function to do it:

    Here is the simple JS approach:

    <% remote_form_for membership do |f| %>
        <%= f.select :permissions, [['none', 0], ['admin', 9]], {}, :onchange => 'this.form.submit()' %>
    <% end %>
    

    The other way would be something like this, and would eschew the form builder (and this may have a couple syntax errors):

    <%= select_tag(:permissions, [['none', 0], ['admin', 9]], {:onchange => "#{remote_function(:url  => permissions_path(:story_id => story,
                 :with => "'permission='+value")}"})
    

    Personally I prefer the former.

提交回复
热议问题