编辑的时候下拉列表框中选中的选项提交不了,而文本框中的选项提交后就得到更新,哪位大神可以帮忙分析一下
def edit
if KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::BrandKeyWordList'
@key_word_list = KeyWordLists::BrandKeyWordList.find_by_id(params[:id])
elsif KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::TaxonKeyWordList'
@key_word_list = KeyWordLists::TaxonKeyWordList.find_by_id(params[:id])
elsif KeyWordList.find_by_id(params[:id])[:type] =='KeyWordLists::GenderKeyWordList'
@key_word_list = KeyWordLists::GenderKeyWordList.find_by_id(params[:id])
end
@stores = Store.all
@genders = Gender.all
@brands = Brand.all
end
def update
@key_word_list = KeyWordList.find_by_id(params[:id])
id = params[:id]
store = Store.find_by_id(@key_word_list[:store_id])
if KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::BrandKeyWordList'
brand = Brand.find_by_id(@key_word_list[:brand_id])
value_en = params["/key_word_lists/#{id}"][:value_en]
value_cn = params["/key_word_lists/#{id}"][:value_cn]
if(@key_word_list.update(brand:brand,store:store,value_en:value_en,value_cn:value_cn))
redirect_to brand_key_word_lists_path
else
render 'edit'
end
elsif KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::TaxonKeyWordList'
taxon = Taxon.find_by_id(@key_word_list[:taxon_id])
value_cn = params["/key_word_lists/#{id}"][:value_cn]
if(@key_word_list.update(taxon:taxon,store:store,value_cn:value_cn))
redirect_to taxon_key_word_lists_path
else
render 'edit'
end
elsif KeyWordList.find_by_id(params[:id])[:type] =='KeyWordLists::GenderKeyWordList'
gender = Gender.find_by_id(@key_word_list[:gender_id])
value_cn = params["/key_word_lists/#{id}"][:value_cn]
if(@key_word_list.update(gender:gender,store:store,value_cn:value_cn))
redirect_to gender_key_word_lists_path
else
render 'edit'
end
end
end
日志
Started POST "/key_word_lists/40/edit" for 127.0.0.1 at 2015-12-09 14:06:17 +0800
Processing by KeyWordListsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VqhkhPHzbdIQIT63dXW8vcmBwxgalKmGZDZ+ZtYi6Xkw8K+dGiJ/5BC1SHXng09/87ABb5KZI2JBzofICTzS0g==", "/key_word_lists/40"=>{"store_id"=>"2", "brand_id"=>"2", "value_cn"=>"托尔斯泰2", "value_en"=>"托尔斯泰2"}, "id"=>"40"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mKeyWordList Load (0.1ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."id" = ? LIMIT 1 [["id", 40]]
[1m[36mStore Load (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."id" = ? LIMIT 1 [["id", "40"]]
[1m[36mBrand Load (0.0ms)[0m [1mSELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35m (0.1ms)[0m begin transaction
[1m[36m (0.1ms)[0m [1mcommit transaction[0m
Redirected to http://localhost:3000/brand_key_word_lists
Completed 302 Found in 11ms (ActiveRecord: 0.4ms)
Started GET "/brand_key_word_lists" for 127.0.0.1 at 2015-12-09 14:06:18 +0800
Processing by KeyWordListsController#index as HTML
Parameters: {"type"=>"KeyWordLists::BrandKeyWordList"}
[1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mKeyWordList Load (0.1ms)[0m [1mSELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."type" = ? ORDER BY "key_word_lists"."id" ASC LIMIT 1[0m [["type", "KeyWordLists::BrandKeyWordList"]]
[1m[35mKeyWordList Load (0.1ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."type" = ? [["type", "KeyWordLists::BrandKeyWordList"]]
[1m[36mStore Load (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mBrand Load (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mStore Load (0.1ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 2]]
[1m[35mBrand Load (0.1ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 2]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 2]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]]
Rendered key_word_lists/index.html.erb within layouts/application (11.7ms)
Rendered shared/_html5.html.erb (0.1ms)
Rendered shared/_meta.html.erb (0.1ms)
Rendered shared/_alert.html.erb (0.1ms)
Rendered shared/_header.html.erb (0.7ms)
Rendered shared/_footer.html.erb (0.1ms)
Completed 200 OK in 103ms (Views: 100.9ms | ActiveRecord: 0.5ms)
# controller
def edit if KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::BrandKeyWordList' @key_word_list = KeyWordLists::BrandKeyWordList.find_by_id(params[:id]) elsif KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::TaxonKeyWordList' @key_word_list = KeyWordLists::TaxonKeyWordList.find_by_id(params[:id]) elsif KeyWordList.find_by_id(params[:id])[:type] =='KeyWordLists::GenderKeyWordList' @key_word_list = KeyWordLists::GenderKeyWordList.find_by_id(params[:id]) end @stores = Store.all @genders = Gender.all @brands = Brand.all end
def update @key_word_list = KeyWordList.find_by_id(params[:id]) id = params[:id] store = Store.find_by_id(@key_word_list[:store_id]) if KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::BrandKeyWordList' brand = Brand.find_by_id(@key_word_list[:brand_id]) value_en = params["/key_word_lists/#{id}"][:value_en] value_cn = params["/key_word_lists/#{id}"][:value_cn] if(@key_word_list.update(brand:brand,store:store,value_en:value_en,value_cn:value_cn)) redirect_to brand_key_word_lists_path else render 'edit' end elsif KeyWordList.find_by_id(params[:id])[:type] == 'KeyWordLists::TaxonKeyWordList' taxon = Taxon.find_by_id(@key_word_list[:taxon_id]) value_cn = params["/key_word_lists/#{id}"][:value_cn] if(@key_word_list.update(taxon:taxon,store:store,value_cn:value_cn)) redirect_to taxon_key_word_lists_path else render 'edit' end elsif KeyWordList.find_by_id(params[:id])[:type] =='KeyWordLists::GenderKeyWordList' gender = Gender.find_by_id(@key_word_list[:gender_id]) value_cn = params["/key_word_lists/#{id}"][:value_cn] if(@key_word_list.update(gender:gender,store:store,value_cn:value_cn)) redirect_to gender_key_word_lists_path else render 'edit' end end end
#view
<%= f.label :store_id, "店铺" %>
<%= f.collection_select(:store_id, @stores, :id,:name,:class => 'form-control')%>
# your logs
Started POST "/key_word_lists/40/edit" for 127.0.0.1 at 2015-12-09 14:06:17 +0800 Processing by KeyWordListsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"VqhkhPHzbdIQIT63dXW8vcmBwxgalKmGZDZ+ZtYi6Xkw8K+dGiJ/5BC1SHXng09/87ABb5KZI2JBzofICTzS0g==", "/key_word_lists/40"=>{"store_id"=>"2", "brand_id"=>"2", "value_cn"=>"托尔斯泰 2", "value_en"=>"托尔斯泰 2"}, "id"=>"40"} [1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mKeyWordList Load (0.1ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."id" = ? LIMIT 1 [["id", 40]] [1m[36mStore Load (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."id" = ? LIMIT 1 [["id", "40"]] [1m[36mBrand Load (0.0ms)[0m [1mSELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35m (0.1ms)[0m begin transaction [1m[36m (0.1ms)[0m [1mcommit transaction[0m Redirected to http://localhost:3000/brand_key_word_lists Completed 302 Found in 11ms (ActiveRecord: 0.4ms)
Started GET "/brand_key_word_lists" for 127.0.0.1 at 2015-12-09 14:06:18 +0800 Processing by KeyWordListsController#index as HTML Parameters: {"type"=>"KeyWordLists::BrandKeyWordList"} [1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] [1m[36mKeyWordList Load (0.1ms)[0m [1mSELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."type" = ? ORDER BY "key_word_lists"."id" ASC LIMIT 1[0m [["type", "KeyWordLists::BrandKeyWordList"]] [1m[35mKeyWordList Load (0.1ms)[0m SELECT "key_word_lists".* FROM "key_word_lists" WHERE "key_word_lists"."type" = ? [["type", "KeyWordLists::BrandKeyWordList"]] [1m[36mStore Load (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mBrand Load (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] [1m[36mStore Load (0.1ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 2]] [1m[35mBrand Load (0.1ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 2]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 2]] [1m[36mCACHE (0.0ms)[0m [1mSELECT "stores".* FROM "stores" WHERE "stores"."id" = ? LIMIT 1[0m [["id", 1]] [1m[35mCACHE (0.0ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."id" = ? LIMIT 1 [["id", 1]] Rendered key_word_lists/index.html.erb within layouts/application (11.7ms) Rendered shared/html5.html.erb (0.1ms) Rendered shared/meta.html.erb (0.1ms) Rendered shared/_alert.html.erb (0.1ms) Rendered shared/header.html.erb (0.7ms) Rendered shared/footer.html.erb (0.1ms) Completed 200 OK in 103ms (Views: 100.9ms | ActiveRecord: 0.5ms)
看日志 store_id 已经提交到 action 了,但是 action 里面没有使用它,只有类似的地方:
store = Store.find_by_id(@key_word_list[:store_id])
我猜要的是
store = Store.find_by_id(params["/key_word_lists/#{id}"][:store_id])
PS:这段代码很混乱,不像 Rails 代码。