繁体   English   中英

Simple_form和Rails-多种选择

[英]Simple_form and Rails - multiple choice

我正在尝试使用simple_form制作表单,并且有多个选择部分。 保存所有数据(我有姓名,姓氏,电子邮件...),但是单选按钮始终为空字符串。 如何列出简单的选择并保存选择?

架构:

create_table "greetings" do |t|
  t.string "first_name"
  t.string "last_name"
  t.string "nickname"
  t.string "multiple_choice"
end

控制器:

def new
  @greeting = Greeting.new
end

def create
  @greeting = Greeting.new(greeting_params)
  @greeting.save
end

private
  def greeting_params
    params.require(:greeting).permit(:first_name, :last_name, :nickname, :multiple_choice)
  end

日志:

在2017年1月7日28:17:32:19 +1000处以:: 1的价格开始以:: 1的形式进行POST“ / greeting”发布:GreetingsController#create作为HTML参数:{“ utf8” =>“✓”,“ authenticity_token” =>“。 。“,” greeting“ => {” first_name“ =>” John“,” last_name“ =>” Doe“,”昵称“ =>” Johnny“,” multiple_choice“ =>”“},” multiple_choice“ => “ Name”,“ commit” =>“ Submit”}(0.2ms)BEGINSQL(0.4ms)插入INTO“ greetings”(“ first_name”,“ last_name”,“昵称”,“ multiple_choice”,“ created_at”,“ updated_at “)值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7)返回” id“ [[” first_name“,” John“],[” last_name“,” Doe“],[” nickname“,” Johnny “],[” multiple_choice“,”“],[” created_at“,2017-07-28 07:32:19 UTC],[” updated_at“,2017-07-28 07:32:19 UTC]]

我想通过以下方式做出多项选择:

  • 名称
  • 昵称

我尝试了view.slim

= simple_form_for @contact_form do |f|
= f.input :first_name, label: 'First name', placeholder: 'Enter first name...'
= f.input :last_name, label: 'Last name', placeholder: 'Enter last name...'
= f.input :nickname, label: 'Nickname', placeholder: 'Enter nickname...'
= f.input :multiple_choice, :collection => ['Name'], :as => :radio_buttons
= f.input :multiple_choice, :collection => ['Surname'], :as => :radio_buttons
= f.input :multiple_choice, :collection => ['Nickname'], :as => :radio_buttons

但这会保存一个空字符串。

谢谢!

我找到了单选按钮的解决方案:

view.slim:

p Name
= f.radio_button :call_time, 'first_name'
p Surname
= f.radio_button :call_time, 'last_name'
p Nickname
= f.radio_button :call_time, 'nickname'
= f.button :submit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM