0

I am trying to use simple_form radio button collection to categorize my post, however, it seems the value cannot be read by model.

The following are codes

class CreatePosts < ActiveRecord::Migration
 def change
 create_table :posts do |t|
  t.text :content
  t.integer :category
  t.references :user, index: true, foreign_key: true
  t.references :comment

  t.timestamps null: false
end
end
end 

And while I made the post like the following

<%= simple_form_for:post do |f| %>
   <%= f.collection_radio_buttons :category, [[1,"Joke "],[2,"Gossip "],[3,"option3 "],[4,"option4 "]], :first, :last %>
   <%= f.text_area :content  %>
   <%= f.button :submit, "Post" %>          
<% end %>

No matter what I click on the radio button. the results are the same

Post Load (12.7ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => #<Post id: 17, content: "test again",  category: nil, user_id: 1, created_at: "2017-12-13 05:47:09", updated_at: "2017-12-13 05:47:09">

category: nil

How to input the value by click a radio button?

2 Answers 2

1

You have missed ], change line

<%= f.collection_radio_buttons :category, [[1,"Joke "],[2,"Gossip "],[3,"option3 "],[4,"option4 "], :first, :last %>

with

<%= f.collection_radio_buttons :category, [[1,"Joke "],[2,"Gossip "],[3,"option3 "],[4,"option4 "]], :first, :last %>
Sign up to request clarification or add additional context in comments.

2 Comments

My code on computer is correct. Just miss type a " ] " on stackoverflow when ask this question. The problem is still exist.
can you paste controller code as well. Have you permit category in controller?/
0

your category column is integer type so i think secound argument of array should be integer instead of string, lets try this.

<%= f.collection_radio_buttons :category, [["Joke ",1],["Gossip ",2],["option3 ",3],["option4 ",4]], :first, :last %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.