ruby on rails 4 - How do you use Active Record Enum Radio Buttons in a form? -


In my app, there is a comment section on the articles I want to make the user comment with 3 different options Have the potential To activate it, I am using an active record Enum. Please note that comment sections have been nested under articles.

  resources: article, only: [: index, show] resource: comment ends  

migration:

  Class AddEnumToCommentModel & lt; ActiveRecord :: Migration change def added_column: Comments,: post_as,: integer, default: 0 and end  

comment model:

  enum post_as:% w (Username, Onliner, Anonymous)  

I tried to add it to the content view, but lost I guess I have to do anything in my controller, but not sure.

Attempted to view:

   This comment was restricted from saving: & lt; / H2 & gt; & Lt; Ul & gt; & Lt;% @ comment.errors.full_messages.each do | Message | & Gt%; & Lt; Li & gt; & Lt;% = Message% & gt; & Lt; / Li & gt; & Lt;% end% & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt;% end% & gt; & Lt; Div class = "field" & gt; & Lt; H3 & gt; Fill your comment & lt; / H3 & gt; & Lt;% = f.label: Content% & gt; & Lt; Br> & Lt;% = f.text_area: content% & gt; & Lt; / Div & gt; & Lt; Div class = "post_as" & gt; & Lt; H3 & gt; Choose how you want to post your comment: & lt; / H3 & gt; & Lt;% = f.input: content, post_as: ???, like: radio% & gt; & Lt; / Div & gt; & Lt; Br> & Lt; Div class = "verb" & gt; & Lt;% = f.submit% & gt; & Lt; / Div & gt; & Lt; Br> & Lt;% end% & gt;    

Trains create a class method using a multi-dimensional attribute name when you enum The method used uses a key value of the strings that you define and maps which integers you can do, you can do something like this:

   post_as Keys.each do | Post_as | & Gt%; & Lt;% = f.radio_button: post_as, post_as% & gt; & Lt;% = f.label post_as.to_sym% & gt; & Lt;% end% & gt;  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -