尝试使用 simple_form 单选按钮输入值

Trying to use simple_form radio button to input value

我正在尝试使用 simple_form 单选按钮集合对我的 post 进行分类,但是,模型似乎无法读取该值。

以下是代码

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 

虽然我制作了 post 如下所示

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

不管我点击什么单选按钮。结果是一样的

Post Load (12.7ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT   [["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">

类别:无

如何通过单击单选按钮输入值?

你错过了],换行

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

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

你的 category 列是整数类型所以我认为数组的第二个参数应该是整数而不是字符串,让我们试试这个。

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