Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: f.select returns string not integer

I have form for user registration. User has to select his country via select options. When i submit form i get error Country(#70309119520500) expected, got String(#8039220) Help me please how to convert string to integer so i can insert record in database.

ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
rails -v: Rails 4.1.8

This is my html.rb form

<%= form_for :user, url: users_path do |f| %>
    <div class="field">
      <%= f.label :countries %>
      <% options = options_from_collection_for_select(Country.all, 'id', 'country', 1) %>
      <%=  f.select :country,  options %>
    </div>
<% end %>

This is request

    Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"34/KncZpwhEDYRZuWe09nKmF7DG7+lEu0IBxe9YbKnE=",
 "user"=>{"firm"=>"Chillum doo",
 "name"=>"myname",
 "last_name"=>"mylastname",
 "address"=>"myAdress",
 "post_number"=>"0000",
 "city"=>"myCity",
 "country"=>"1",
 "telephone"=>"000000000",
 "mobile_phone"=>"000000000",
 "user_name"=>"myUserName",
 "email"=>"[email protected]",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Create Account"}

Users controller

class UsersController < ApplicationController


  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save!
      redirect_to '/', notice: 'User registered successfully'
    else
      render 'new'
    end
  end

  private
  def user_params
    params.require(:user).permit(:firm, :name, :last_name, :address, :post_number,
                                 :city, :country, :telephone, :mobile_phone, :user_name,
                                 :email, :password, :password_confirmation)
  end

end
like image 565
Valor_ Avatar asked May 16 '26 12:05

Valor_


1 Answers

Presuming that your User model belongs_to :country, you'll want your form to submit the country_id, not the country. Try something like:

<%=  f.select :country_id,  options %>

You'll need to permit country_id as an attribute in your controller too.

like image 58
Todd Agulnick Avatar answered May 19 '26 01:05

Todd Agulnick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!