问题描述
不知道这里出了什么问题。从内存它用来工作正常,但我最近使用的形式,意识到它已经停止工作的某个地方在构建应用程序的过程。小测试仍然通过它,这可能意味着它是微不足道或直接的形式。
我想获得嵌套的属性 value
传递
<$> p $ p>
has_many:category_item_values,dependent::destroy
accepts_nested_attributes_for:category_item_values
extend FriendlyId
friendly_id:name,use::slugged
category_item_value模型
belongs_to:category_item
category_items / edit.html。 erb
<%= form_for([@ category,@category_item],url:category_items_update_path)do | f | %>
<%= render'shared / error_messages',object:f.object%>
<%= f.fields_for:category_item_values do | item_value | %>
< div class =value_fields>
<%= item_value.object.key%>< br>
<%= item_value.label:value%>
<%= item_value.text_field:value%>
< / div>
<%end%>
<%= f.submit保存%>
<%end%>
category_item控制器
def edit
@guide = Guide.friendly.find(params [:guide_id])
@category = Category.friendly.find params [:category_id])
@category_item = @ category.category_items.friendly.find params [:id]
end
def update
@guide = Guide.friendly.find(params [:guide_id])
@category = Category.friendly.find(params [:category_id])
@category_item = @category .category_items.friendly.find params [:id]
如果@ category_item.update(item_params)
flash [:success] =已更新,感谢您的贡献!
redirect_to category_items_show_path(@guide,@category,@category_item)
else
渲染'edit'
结束
结束
$ b b private
def item_params
params.require(:category_item).permit(:name,category_item_values_attributes:[:id,:value])
end
提交表单时传递的参数
参数:{utf8=>✓,authenticity_token=>O / + jbpxxY9ACV1 + M7SQoYXHGvhOlgT / SufGoFpjTKaxqewnQ0IKRJWF + kxoE99DCaiKYPVkKtctSrQ0hPegIEw ==,category_item=> ; {category_item_values_attributes=> {0=> {id=>39},1=> {id=>40},2=> ; {id=>41},3=> {id=>42},4=> {id=>43 5=> {id=>44},6=> {id=>45},7=> {id=>46 },8=> {id=>47},9=> {id=>48},10=> {id= >49}}},commit=>保存,guide_id=>dungeon-boss,category_id="heroes,id=>smok }
通过的微型测试
testmod editing all categories items successfullydo
log_in_as(@user)
get category_items_edit_path(@guide,@category,@category_item )
assert_template'category_items / edit'
assert_select'form.edit_category_item'
assert CategoryItemValue.find(1).value =='simmv'#confirm edit before
patch category_items_update_path (params:{key_id:1}),category_item:{category_item_values_attributes:[id:1,value:333]}
assert CategoryItemValue.find(1).value =='333'#confirm value changed after编辑
follow_redirect!
assert_template'category_items / show'
assert_select'div.alert'
end
$ b b
已经过几次,但不知道如何获得值
嵌套属性传递到params和db从表单提交。
categroy_item_values模式
create_tablecategory_item_values,force::cascade do | t |
t.stringkey
t.integerkey_type
t.textvalue
t.integercategory_item_key_id
t.integercategory_item_id
t.datetimecreated_at,null:false
t.datetimeupdated_at,null:false
end
*以下是加载网页然后提交表单的日志
GET/ guides / dungeon-boss / heroes / rock / editfor :: 1在2016-02-16 21:59:16 +1100
处理CategoryItemsController#编辑为HTML
参数:{ guide_id=>dungeon-boss,category_id=>heroes,id=>rock}
[1m [35mUser Load 。* FROMusersWHEREusers。id=? LIMIT 1 [[id,1]]
[1m [36mGuide Load(0.1ms)[0m [1mSELECTguides。* FROMguidesWHEREguides ORDER BYguides。idASC LIMIT 1 [0m [[slug,dungeon-boss]]
[1m [35m类别加载(0.1ms)类别WHERE类别。slug=? ORDER BYcategories。idASC LIMIT 1 [[slug,heroes]]
[1m [36mGameModsRelationship存在(0.1ms)[0m [1mSELECT 1作为一个fromgame_mods_relationshipsWHEREgame_mods_relationships 。user_id=? ANDgame_mods_relationships。category_id= 7 LIMIT 1 [0m [[user_id,1]]
[1m [35mCategoryItem Load(0.2ms)[0m SELECTcategory_items。* FROMcategory_itemsWHERE category_items。category_id=? ANDcategory_items。slug=? ORDER BYcategory_items。idASC LIMIT 1 [[category_id,3],[slug,rock]]
[1m [36mCACHE(0.0ms)[0m [1mSELECT 1 AS one FROMgame_mods_relationshipsWHEREgame_mods_relationships。user_id=? ANDgame_mods_relationships。category_id= 7 LIMIT 1 [0m [[user_id,1]]
呈现的共享/ _error_messages.html.erb(0.1ms)
[1m [35mCategoryItemValue Load ms)[0m SELECTcategory_item_values。* FROMcategory_item_valuesWHEREcategory_item_values。category_item_id=?
渲染的布局/应用程序中的category_items / edit.html.erb(19.5ms)
渲染的布局/ _shim.html.erb(0.0ms)
渲染layouts / _header.html.erb(0.5ms)
呈现的布局/ _footer.html.erb(0.2ms)
在230ms内完成200 OK(视图:221.5ms | ActiveRecord:0.8ms)
开始PATCH/ guides / dungeon-boss / heroes / rockfor :: 1 at 2016-02-16 21:59:21 +1100
按CategoryItemsController处理#update as HTML
参数:{utf8=>✓,authenticity_token=>fT4HylfaCf9ONVlRYLKP9jP4nlerxMZoyyYJkx0mDkwsuq10Gyn7Ci0clceJYXdVKBy4eVdPTHEgeqykuB0v8w ==,category_item=> {category_item_values_attributes=> {0=& {id=>28},1=> {id=>29},2=> {id=>30 => {id=>31},4=> {id=>32},5=> {id=& },6=> {id=>34},7=> {id=>35},8=> {id=> ;36},9=> {id=>37},10=> {id=>38},11=& =>39},12=> {id=>40},13=& > {id=>42},15=> {id=>43},16=> {id=>44 17=> {id=>45},18=> {id=>46},19=> {id=& 47},20=> {id=>48},21=> {id=>49},22=& =50},23=> {id=>51},24=> {id=>52},25=& {id=>53},26=> {id="54},27=> {id=>55 => {id=>56},29=> {id=>57}}},commit=>Save,guide_id= "dungeon-boss,category_id=>heroes,id=>rock}
[1m [36mUser Load(0.1ms)[0m [1mSELECTusers FROMusersWHEREusers。id=? LIMIT 1 [0m [[id,1]]
[1m [35mGuide Load(0.1ms)[0m SELECTguides。* FROMguidesWHEREguides。 ORDER BYguides。idASC LIMIT 1 [[slug,dungeon-boss]]
[1m [36mGameModsRelationship存在(0.1ms)[0m [1mSELECT 1作为一个fromgame_mods_relationshipsWHERE game_mods_relationships。user_id=? ANDgame_mods_relationships。category_id= 7 LIMIT 1 [0m [[user_id,1]]
[1m [35mCategory Load(0.1ms)[0m SELECTcategories。* FROMcategoriesWHERE类别。slug=? ORDER BYcategories。idASC LIMIT 1 [[slug,heroes]]
[1m [36mCategoryItem Load(0.1ms)[0m [1mSELECTcategory_items。* FROMcategory_itemsWHERE category_items。category_id=? ANDcategory_items。slug=? ORDER BYcategory_items。idASC LIMIT 1 [0m [[category_id,3],[slug,rock]]
[1m [35mCACHE(0.0ms)[0m SELECT 1 AS one FROMgame_mods_relationshipsWHEREgame_mods_relationships。user_id=? ANDgame_mods_relationships。category_id= 7 LIMIT 1 [[user_id,1]]
[1m [36m(0.0ms)[0m [1mbegin transaction [0m
[1m [35mCategoryItemValue Load 0.3ms)[0m SELECTcategory_item_values。* FROMcategory_item_valuesWHEREcategory_item_values。category_item_id=? ANDcategory_item_values。idIN(28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 48,49,50,51,52,53,54,55,56,57)[[category_item_id,10]]
[1m [36mCategoryItem Exists(0.1ms)[0m [1mSELECT 1 AS one FROM category_itemsWHERE(LOWER(category_items。name)= LOWER('rock')ANDcategory_items。id!= 10 ANDcategory_items。guide_id= 7)LIMIT 1 [0m
[1m [35m(0.0ms)[0m提交事务
重定向到http:// localhost:3000 / guides / dungeon-boss / heroes / rock
完成302找到16ms(ActiveRecord:0.8 ms)
一切似乎都很好。此外,当我手动给 value
中的一个值时,它显示在要编辑的表单输入中,因此表单似乎正常工作。它只是当我点击提交它不添加值
到属性参数散列。
在日志中,如果有什么混乱,你认为可能会导致问题只是问(我有一个坏习惯,假设人们知道一部分的随机添加,他们看不到)。
解决方案 <%= form_for [@category,@category_item],url:category_items_update_path do | f | %>
<%= render'shared / error_messages',object:f.object%>
<%= f.fields_for:category_item_values do | item_value | %>
<%= item_value.object.key%>
<%= item_value.label:value%>
<%= item_value.text_field:value%>
<%end%>
<%= f.submit保存%>
<%end%>
无论何时创建代码,您都需要确保它。
此代码是好的;可能的问题是,你使用来自其他视图/操作的过期代码。如果您张贴了整个控制台日志的请求,我们可以给你一个更合适的答案。
guide_id=>dungeon-boss,category_id=>heroes,id=>smok
这是从你的参数。这看起来不像 category_items_update_path
;也许它将帮助您识别正在使用的视图。
No idea whats going wrong here. From memory it used to work fine but I just recently used the form and realised it has stopped working somewhere in the process of building the app. Mini test still passes it which could mean it is something trivial or on the form directly.
I'm trying to get the nested attribute value
to pass on the from submit
category_item model
has_many :category_item_values, dependent: :destroy
accepts_nested_attributes_for :category_item_values
extend FriendlyId
friendly_id :name, use: :slugged
category_item_value model
belongs_to :category_item
category_items/edit.html.erb
<%= form_for([@category, @category_item], url: category_items_update_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.fields_for :category_item_values do |item_value| %>
<div class="value_fields">
<%= item_value.object.key %><br>
<%= item_value.label :value %>
<%= item_value.text_field :value %>
</div>
<% end %>
<%= f.submit "Save"%>
<% end %>
category_item controller
def edit
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@category_item = @category.category_items.friendly.find params[:id]
end
def update
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@category_item = @category.category_items.friendly.find params[:id]
if @category_item.update (item_params)
flash[:success] = "Updated. Thanks for contributing!"
redirect_to category_items_show_path(@guide, @category, @category_item)
else
render 'edit'
end
end
private
def item_params
params.require(:category_item).permit(:name, category_item_values_attributes: [:id, :value])
end
parpamters passed when the form is submitted
Parameters: {"utf8"=>"✓", "authenticity_token"=>"O/+jbpxxY9ACV1+M7SQoYXHGvhOlgT/SufGoFpjTKaxqewnQ0IKRJWF+kxoE99DCaiKYPVkKtctSrQ0hPegIEw==", "category_item"=>{"category_item_values_attributes"=>{"0"=>{"id"=>"39"}, "1"=>{"id"=>"40"}, "2"=>{"id"=>"41"}, "3"=>{"id"=>"42"}, "4"=>{"id"=>"43"}, "5"=>{"id"=>"44"}, "6"=>{"id"=>"45"}, "7"=>{"id"=>"46"}, "8"=>{"id"=>"47"}, "9"=>{"id"=>"48"}, "10"=>{"id"=>"49"}}}, "commit"=>"Save", "guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"smok"}
The mini test that passes
test "mod editing all category items successfully" do
log_in_as(@user)
get category_items_edit_path(@guide, @category, @category_item)
assert_template 'category_items/edit'
assert_select 'form.edit_category_item'
assert CategoryItemValue.find(1).value == 'simmv' #confirm value before edit
patch category_items_update_path(params: {key_id: 1}), category_item: { category_item_values_attributes: [id: 1, value: "333"]}
assert CategoryItemValue.find(1).value == '333' #confirm value changed after edit
follow_redirect!
assert_template 'category_items/show'
assert_select 'div.alert'
end
Been over it a few times but cant figure out how to get the value
nested attribute to pass into the params and db from the form submit.
categroy_item_values schema
create_table "category_item_values", force: :cascade do |t|
t.string "key"
t.integer "key_type"
t.text "value"
t.integer "category_item_key_id"
t.integer "category_item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
*Here is the log of loading the page then submitting the form
Started GET "/guides/dungeon-boss/heroes/rock/edit" for ::1 at 2016-02-16 21:59:16 +1100
Processing by CategoryItemsController#edit as HTML
Parameters: {"guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"rock"}
[1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mGuide Load (0.1ms)[0m [1mSELECT "guides".* FROM "guides" WHERE "guides"."slug" = ? ORDER BY "guides"."id" ASC LIMIT 1[0m [["slug", "dungeon-boss"]]
[1m[35mCategory Load (0.1ms)[0m SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? ORDER BY "categories"."id" ASC LIMIT 1 [["slug", "heroes"]]
[1m[36mGameModsRelationship Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 7 LIMIT 1[0m [["user_id", 1]]
[1m[35mCategoryItem Load (0.2ms)[0m SELECT "category_items".* FROM "category_items" WHERE "category_items"."category_id" = ? AND "category_items"."slug" = ? ORDER BY "category_items"."id" ASC LIMIT 1 [["category_id", 3], ["slug", "rock"]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 7 LIMIT 1[0m [["user_id", 1]]
Rendered shared/_error_messages.html.erb (0.1ms)
[1m[35mCategoryItemValue Load (0.2ms)[0m SELECT "category_item_values".* FROM "category_item_values" WHERE "category_item_values"."category_item_id" = ? [["category_item_id", 10]]
Rendered category_items/edit.html.erb within layouts/application (19.5ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 230ms (Views: 221.2ms | ActiveRecord: 0.8ms)
Started PATCH "/guides/dungeon-boss/heroes/rock" for ::1 at 2016-02-16 21:59:21 +1100
Processing by CategoryItemsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"fT4HylfaCf9ONVlRYLKP9jP4nlerxMZoyyYJkx0mDkwsuq10Gyn7Ci0clceJYXdVKBy4eVdPTHEgeqykuB0v8w==", "category_item"=>{"category_item_values_attributes"=>{"0"=>{"id"=>"28"}, "1"=>{"id"=>"29"}, "2"=>{"id"=>"30"}, "3"=>{"id"=>"31"}, "4"=>{"id"=>"32"}, "5"=>{"id"=>"33"}, "6"=>{"id"=>"34"}, "7"=>{"id"=>"35"}, "8"=>{"id"=>"36"}, "9"=>{"id"=>"37"}, "10"=>{"id"=>"38"}, "11"=>{"id"=>"39"}, "12"=>{"id"=>"40"}, "13"=>{"id"=>"41"}, "14"=>{"id"=>"42"}, "15"=>{"id"=>"43"}, "16"=>{"id"=>"44"}, "17"=>{"id"=>"45"}, "18"=>{"id"=>"46"}, "19"=>{"id"=>"47"}, "20"=>{"id"=>"48"}, "21"=>{"id"=>"49"}, "22"=>{"id"=>"50"}, "23"=>{"id"=>"51"}, "24"=>{"id"=>"52"}, "25"=>{"id"=>"53"}, "26"=>{"id"=>"54"}, "27"=>{"id"=>"55"}, "28"=>{"id"=>"56"}, "29"=>{"id"=>"57"}}}, "commit"=>"Save", "guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"rock"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 1]]
[1m[35mGuide Load (0.1ms)[0m SELECT "guides".* FROM "guides" WHERE "guides"."slug" = ? ORDER BY "guides"."id" ASC LIMIT 1 [["slug", "dungeon-boss"]]
[1m[36mGameModsRelationship Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 7 LIMIT 1[0m [["user_id", 1]]
[1m[35mCategory Load (0.1ms)[0m SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? ORDER BY "categories"."id" ASC LIMIT 1 [["slug", "heroes"]]
[1m[36mCategoryItem Load (0.1ms)[0m [1mSELECT "category_items".* FROM "category_items" WHERE "category_items"."category_id" = ? AND "category_items"."slug" = ? ORDER BY "category_items"."id" ASC LIMIT 1[0m [["category_id", 3], ["slug", "rock"]]
[1m[35mCACHE (0.0ms)[0m SELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 7 LIMIT 1 [["user_id", 1]]
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mCategoryItemValue Load (0.3ms)[0m SELECT "category_item_values".* FROM "category_item_values" WHERE "category_item_values"."category_item_id" = ? AND "category_item_values"."id" IN (28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57) [["category_item_id", 10]]
[1m[36mCategoryItem Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "category_items" WHERE (LOWER("category_items"."name") = LOWER('rock') AND "category_items"."id" != 10 AND "category_items"."guide_id" = 7) LIMIT 1[0m
[1m[35m (0.0ms)[0m commit transaction
Redirected to http://localhost:3000/guides/dungeon-boss/heroes/rock
Completed 302 Found in 16ms (ActiveRecord: 0.8ms)
Everything seems to work fine. Also when I manually give value
a value in the db it shows up in the forms input to be edited, so the form seems to be working correctly. Its just when I hit submit it wont add value
into the attributed parameters hash.
There is a bit going on in the log so if something confuses that you think might cause the problem just ask (I have a bad habit of assuming people know about parts of a random add they cant see).
解决方案 <%= form_for [@category, @category_item], url: category_items_update_path do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.fields_for :category_item_values do |item_value| %>
<%= item_value.object.key %>
<%= item_value.label :value %>
<%= item_value.text_field :value %>
<% end %>
<%= f.submit "Save"%>
<% end %>
Whenever you create code, you need to ensure it's indented properly.
This code is good; the likely issue is that you're using outdated code from some other view / action. If you posted your entire console log for the request, we could give you a more appropriate answer.
"guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"smok"
This is from your params. This does not look like category_items_update_path
; perhaps it will help you identify the view that's being used.
这篇关于Rails嵌套表单只在表单提交时传递id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!