嗯 对 直接粘贴 include Mongoid::Document 就可以用了,要是名称对不上可以加上 store_in collection: "citizens", database: "other"指定 即可解决
新手学习了,受益匪浅
额 查了半天才发现读取的 csv 文件中间被打开过又重新存过,上面的方法是对的,已解决!
请问大神,这样写后 csv 开头的中文怎么错乱了呢 这是实际 csv 中的中文 这是读 csv 出来的中文
嗯 是呢 已解决 谢谢大神
ok 谢谢
打不开呢
谢谢大神每次都来指教
哦 谢谢大神 我改一下试试
# 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)
@rei 谢谢大神 这个格式化好像不太会用
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)
请问下拉列表框中表示当前项被选中的是那个参数呢 应该是选中的没有没有被读到
谢谢大神
@so_zengtao 是继承关系 单表继承 大神知不知道路由该怎么写
如果 type 是自动的 那路由怎么写呢
比如说下面的 BrandKeyWordList 继承了 KeyWordLists,在 controller 中怎么更新删除刚添加的这条记录呢? KeyWordLists::BrandKeyWordList.create!(brand: brand, store: store_2, value_cn: '耐克', value_cn: '耐克',value_en: 'nike')