简体   繁体   中英

Simple_form and Rails - multiple choice

I'm trying to make a form with simple_form and there is multiple choice part. All the data is saved (I have name, surname, email...), but the multiple choice radio button is always an empty string. How do I make a simple list of choices and save that choice?

Schema:

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

Controller:

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

Logs:

Started POST "/greeting" for ::1 at 2017-07-28 17:32:19 +1000 Processing by GreetingsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "greeting"=>{"first_name"=>"John", "last_name"=>"Doe", "nickname"=>"Johnny", "multiple_choice"=>""}, "multiple_choice"=>"Name", "commit"=>"Submit"} (0.2ms) BEGINSQL (0.4ms) INSERT INTO "greetings" ("first_name", "last_name", "nickname", "multiple_choice", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "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]]

I want to make this multiple choice by:

  • Name
  • Surname
  • Nickname

I tried in 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

But it saves an empty string.

Thanks!

I found a solution for 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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