【发布时间】:2016-02-15 01:08:25
【问题描述】:
不知道这里出了什么问题。从记忆中它过去工作正常,但我最近才使用该表单并意识到它在构建应用程序的过程中已停止工作。迷你测试仍然通过它,这可能意味着它是微不足道的或直接在表单上。
我正在尝试让嵌套属性value 传递来自提交
category_item 模型
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 "Save"%>
<% 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]
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
提交表单时传递的参数
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"}
通过的小测试
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
看过几次,但不知道如何让value嵌套属性从表单提交传递到参数和数据库。
categroy_item_values 架构
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
*这里是加载页面然后提交表单的日志
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)
似乎一切正常。此外,当我在数据库中手动给value 一个值时,它会显示在要编辑的表单输入中,因此表单似乎工作正常。就在我点击提交时,它不会将value 添加到属性参数哈希中。
日志中发生了一些事情,所以如果您认为可能导致问题的内容令人困惑,请询问(我有一个坏习惯,即假设人们知道他们看不到的随机添加的部分内容)。
【问题讨论】:
-
奇怪。您是否使用了与您在编辑器中查看的代码不同的版本(例如不同的 git 分支)?在您的表单中,
form_for的<% end %>是下一行,还是您没有向表单显示的更多内容? -
以为我可能无意中从嵌套表单中删除了另一个
<% end %>,但我只是忘记将其粘贴到我的问题中。修正了问题。 -
category_item_values是否存储在数据库中?还是他们只是拥有:id、:key和:value字段?更好的是,您可以发布category_item_values的架构吗? -
据我所见,
:value属性至少出现在params哈希中,一切看起来都很好。除了确保您正在查看params哈希以获取正确的请求,并且可能只是重新启动服务器以查看是否修复了某些问题之外,我什么都没有。对不起。 -
谢谢。即使说你什么都没有,也意味着它很可能是我忽略的应用程序设置的一些更深层次的问题,并确认它不仅仅是一个明显的编码问题。
标签: ruby-on-rails ruby-on-rails-4 parameters controller nested-forms