未使用ruby on rails中的city-state gem存储在数据库中的状态

时间:2017-12-06 21:56:28

标签: ruby-on-rails ruby drop-down-menu rubygems

所以我在我的网络应用程序中使用一个名为city-state gem的gem来填充美国50个州的下拉列表,但是我无法将所选值保存在数据库中,也许我在这里遗漏了一些东西,我有以下代码:

<%= 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 %>

我尝试使用

<%= f.select :state, value %> 

但这会给我一个空的下拉列表,任何帮助赞赏。谢谢提前。

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"=>"han@gmail.com", "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", "han@saveup.com"], ["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", "han@saveup.com"], 
["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)

2 个答案:

答案 0 :(得分:2)

不是手动构建表单标签和选项而不是让rails更干净吗?这也可以为您修复名称问题:

<%= 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还会处理以这种方式编辑时填充当前值。

答案 1 :(得分:1)

我猜测(因为您没有向我们展示您的控制器代码),您希望state成为user参数的一部分。但是,正如你所看到的,它不是:

{
  "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"=>"han@gmail.com", 
    "password"=>"[FILTERED]", 
    "password_confirmation"=>"[FILTERED]", 
    "tandc"=>"true"
  }, 
  "state"=>"MD", 
  "commit"=>"Sign Up"
}

认为是因为你做了:

<select name="form[state]">

您可能需要以下内容:

<select name="user[state]">

我不确定这种语法,所以你不得不喋喋不休。