How do you use Active Record Enum Radio Buttons in a form?
Asked Answered
M

4

16

In my app, there is a comment section on articles. I'd like the user to have the ability to comment with 3 different options. To activate this, I am using an Active Record Enum. Please note that the comment sections is nested under the articles.

resources :articles, only: [:index, :show] do
  resources :comments
end

Migration:

class AddEnumToCommentModel < ActiveRecord::Migration
  def change
    add_column :comments, :post_as, :integer, default: 0
  end
end

Comment model:

enum post_as: %w(username, oneliner, anonymous)

I attempted to add this to the content view, but lost. I am guessing I also have to do something in my controller but not sure.

Attempted view :

<%= form_for([@article, @comment]) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @comment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <h3>Fill in your comment</h3>
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>

  <div class="post_as">
    <h3> Choose how you want to post your comment :</h3>
    <%= f.input :content, post_as: ???, as: :radio %>
  </div>

  <br>

  <div class="actions">
    <%= f.submit %>
  </div>

  <br>
<% end %>
Montemayor answered 31/7, 2014 at 20:38 Comment(0)
O
23

Rails creates a class method using the pluralized attribute name when you use enum. The method returns a key value pair of strings you've defined and what integers they map to. So, you could do something like this:

<% Comment.post_as.keys.each do |post_as| %>
  <%= f.radio_button :post_as, post_as %>
  <%= f.label post_as.to_sym %>
<% end %>
Orthodoxy answered 24/10, 2014 at 20:28 Comment(0)
K
7

There's also collection_radio_buttons, which is more succinct than the other options.

<%= f.collection_radio_buttons :post_as, Comment.post_as, :second, :first %>

Those last two arguments specify how to get the input's value and label text. In your example Comment.post_as produces a hash of enum key names to the underlying integer, so we can grab those using :second for the integer and :first for the name — easy!

Here's what that produces:

<input type="radio" value="0" name="comment[post_as]" id="comment_post_as_0">
<label for="comment_post_as_0">username</label>
# Etc.

You can also customize the HTML by passing a block, which is my preferred way to create enum radio buttons with clickable labels:

<%= f.collection_radio_buttons :post_as, Comment.post_as, :second, :first do |b|
  b.label { b.radio_button + b.text }
end %>
Kellda answered 15/3, 2018 at 20:38 Comment(0)
C
6

An addition to xxyyxx's answer, if you want the labels to be clickable as well:

<% Comment.post_as.keys.each do |post_as| %>
  <%= f.radio_button :post_as, post_as %>
  <%= f.label "#{:post_as}_#{post_as.parameterize.underscore}", post_as %>
<% end %>
Constitutionalism answered 31/3, 2017 at 8:32 Comment(0)
B
4

In the view instead of

<%= f.input :content, post_as: ???, as: :radio %>

you could have

<%= f.radio_button(:post_as, "username") %>
<%= label(:post_as, "Username") %>
<%= f.radio_button(:post_as, "oneliner") %>
<%= label(:post_as, "Oneline") %>
<%= f.radio_button(:post_as, "anonymous") %>
<%= label(:post_as, "Anonymous") %>

Source: http://guides.rubyonrails.org/form_helpers.html#radio-buttons

Basseterre answered 31/7, 2014 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.