0
votes

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!

1
Post your controller code along with logs that are fired upon form submitPavan
Post the full logs please including everything.Pavan
I pasted the logs as requested, I hope it helps. Thanks!Tom Bomb
Can you post your simple form that you have set up? the entire view?Steph Rose
I updated it, I hope it helps.Tom Bomb

1 Answers

0
votes

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