Reputation: 43
so I am using a gem called city-state gem in my web application to populate a dropdown list with the 50 states of US, but I am unable to save the selected value in the database,maybe I am missing something here, I have the following code:
<%= form_for(resource, as: resource_name, :class=>'well form-horizontal' ,:style=>"margin-top:30px",url: registration_path(resource_name)) do |f| %>
<select name="form[state]">
<% CS.states(:us).each do |key, value| %>
<option value="<%= key %>"><%= value %></option>
<% end %>
</select>
<% end %>
I tried using
<%= f.select :state, value %>
but that would give me an empty dropdown list, any help appreciated.Thanks in advance.
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"w1dXvna7zYZ96f9pNtCdm3ruIHu/J9+kXZ6rxoPy+7QJCqTDAs+Y1ZcOgUvRiJEEZpyJ3S0qpLEbfoKEr+15bw==", "user"=>{"firstname"=>"Han", "lastname"=>"Park", "gender"=>"Male", "dob(1i)"=>"1996", "dob(2i)"=>"12", "dob(3i)"=>"6", "ssn"=>"1432229824", "driver_license"=>"D768765764", "street_address"=>"1000 north", "city"=>"New York", "zip"=>"60098", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "tandc"=>"true"}, "state"=>"MD", "commit"=>"Sign Up"}
DEPRECATION WARNING: ssn is not an attribute known to Active Record. This
behavior is deprecated and will be removed in the next version of Rails. If
you'd like ssn to be managed by Active Record, add `attribute :ssn to your
class. (called from block in attr_encrypted at /Users/reynold/.rvm/gems/ruby-
2.2.2/gems/attr_encrypted-
3.0.3/lib/attr_encrypted/adapters/active_record.rb:75)
(0.3ms) BEGIN
User Exists (0.8ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1
LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]]
SQL (6.2ms) INSERT INTO "users" ("email", "encrypted_password", "created_at",
"updated_at", "firstname", "lastname", "gender", "encrypted_ssn",
"encrypted_ssn_iv", "street_address", "city", "zip", "driver_license",
"tandc", "dob") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12,
$13, $14, $15) RETURNING "id" [["email", "[email protected]"],
["encrypted_password",
"$2a$11$asC6.rG.aYUXJa8Ps.Gbeu3FzTFw64urmIB2KK9rxj94sH71gXaYm"],
["created_at", "2017-12-06 22:17:55.383121"], ["updated_at", "2017-12-06
22:17:55.383121"], ["firstname", "Han"], ["lastname", "Park"], ["gender",
"Male"], ["encrypted_ssn", "eWjL/lDpivR3T9cXx1w6rg==\n"], ["encrypted_ssn_iv",
"Rt/5Tl6PkwvqOjDC9nN1ng==\n"], ["street_address", "1000 north"], ["city",
"New York"], ["zip", "60098"], ["driver_license", "D768765764"], ["tandc",
"t"], ["dob", "1996-12-06"]]
(0.6ms) COMMIT
(0.3ms) BEGIN
SQL (2.4ms) UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" =
$2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5,
"updated_at" = $6 WHERE "users"."id" = $7 [["sign_in_count", 1],
["current_sign_in_at", "2017-12-06 22:17:55.397080"], ["last_sign_in_at",
"2017-12-06 22:17:55.397080"], ["current_sign_in_ip", "127.0.0.1/32"],
["last_sign_in_ip", "127.0.0.1/32"], ["updated_at", "2017-12-06
22:17:55.398231"], ["id", 18]]
(0.5ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 276ms (ActiveRecord: 11.1ms)
Started GET "/" for 127.0.0.1 at 2017-12-06 16:17:55 -0600
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
ORDER BY "users"."id" ASC LIMIT $2 [["id", 18], ["LIMIT", 1]]
Processing by HomeController#roothome as HTML
Rendering home/roothome.html.erb within layouts/application
Rendered shared/_comparechart.html.erb (1.0ms)
Rendered shared/_disclaimer.html.erb (0.7ms)
Rendered home/roothome.html.erb within layouts/application (8.1ms)
Rendered _navbar.html.erb (4.6ms)
Completed 200 OK in 478ms (Views: 474.8ms | ActiveRecord: 0.0ms)
Upvotes: 0
Views: 521
Reputation: 4877
Instead of building out the form tag and options manually wouldn’t it be cleaner to let rails do it? This would also have fixed the name issue for you:
<%= form_for(resource, as: resource_name, :class=>'well form-horizontal' ,:style=>"margin-top:30px",url: registration_path(resource_name)) do |f| %>
<%= f.select :state, CS.states(:us) %>
<% end %>
Rails will also handle populating the current value when editing for you this way.
Upvotes: 2
Reputation: 20263
I'm guessing (since you didn't show us your controller code) that you expect state
to be a part of the user
params. But, as you can see, it is not:
{
"utf8"=>"✓",
"authenticity_token"=>"w1dXvna7zYZ96f9pNtCdm3ruIHu/J9+kXZ6rxoPy+7QJCqTDAs+Y1ZcOgUvRiJEEZpyJ3S0qpLEbfoKEr+15bw==",
"user"=>{
"firstname"=>"Han",
"lastname"=>"Park",
"gender"=>"Male",
"dob(1i)"=>"1996",
"dob(2i)"=>"12",
"dob(3i)"=>"6",
"ssn"=>"1432229824",
"driver_license"=>"D768765764",
"street_address"=>"1000 north",
"city"=>"New York",
"zip"=>"60098",
"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"tandc"=>"true"
},
"state"=>"MD",
"commit"=>"Sign Up"
}
I think it's because you do:
<select name="form[state]">
You probably want something like:
<select name="user[state]">
I'm not sure about that syntax, so you'll have to monkey with it.
Upvotes: 1