yellowskull
yellowskull

Reputation: 177

Rails loop rename

I have a loop which contains (0,1,2) as options listed in my database.

In my view I have a checkbox option listing 0,1,2 as selected options, but I wish to rename these in the view. For example 0 = Option 1, 1 = Option 2, 2 = Option 3.

View

<%= form_for :search, url: search_path do |f| %>
<% @check.each do |c| %>
<%= check_box :check, c.options, {:checked => true}, class: "checkbox inline" %> <%= c.options %>
<% end %>
<%= f.button :submit, class: 'btn btn-success' %>
<% end %>

Upvotes: 1

Views: 61

Answers (1)

danilodeveloper
danilodeveloper

Reputation: 3880

You can use check_box_tag, for example:

<ul>
  <% @check.each do |c| %>
      <li>
        <%= check_box_tag "check_options[#{c.id}]", c.id, :name => "check_options[]" -%>
        <%= h c.name -%>
      </li>
  <% end %>
</ul>

Basically you need to pass the options like an array check_options[] and the identification of this option check.id. The check.name is the checkbox label.

I hope this helps.

Upvotes: 1

Related Questions