简体   繁体   中英

Persist radio_button value on form_tag submit get method

I think this is quite simple, but really struggling here:

<%= form_tag(admin_articles_path, method: "get") do %>

  <p><%= radio_button_tag(:filter, "all") %>
  <%= label_tag(:filter, "All") %></p>

  <p><%= radio_button_tag(:filter, "pub") %>
  <%= label_tag(:filter, "Published") %></p>

  <p><%= radio_button_tag(:filter, "unpub") %>
  <%= label_tag(:filter, "Unpublished") %></p>

  <p><%= radio_button_tag(:filter, "feat") %>
  <%= label_tag(:filter, "Featured") %></p>

 <p> <%= submit_tag("Show", class: "btn btn-sm btn-primary") %></p>

   <% end %>

I have some sorting logic in my controller, and when I submit this I want the radio_button value to persist to the new view. How do I do this?

Thank!

You want to do this sort of thing. For example, with the all option...

<p><%= radio_button_tag(:filter, "all", params[:filter] == "all") %>

The 3rd param is a boolean that says whether or not it is checked.

You will also need something to uncheck a radio. I use this...

<p><%= radio_button_tag(:filter, '', params[:filter].nil?) %>

In your receiving controller action:

@filter = params[:filter]

You then have access to the radio button selection in your respective view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM