Chionn K
Chionn K

Reputation: 31

Trying to use simple_form radio button to input value

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?

Upvotes: 0

Views: 235

Answers (2)

Anand
Anand

Reputation: 6531

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 %>

Upvotes: 0

puneet18
puneet18

Reputation: 4427

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 %>

Upvotes: 1

Related Questions