不知道这里出了什么问题。从内存来看它曾经工作正常,但我最近才使用该表单,并意识到它已经停止在构建应用程序的过程中的某个地方工作。迷你测试仍然通过它,这可能意味着它是微不足道的或直接在表格上。
我正在尝试让嵌套属性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
提交表单时传递的parpamters
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
嵌套属性从表单提交传递到params和db。
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)
一切似乎都很好。此外,当我在db中手动提供value
一个值时,它会显示在要编辑的表单输入中,因此表单似乎正常工作。就在我点击提交时,它不会将value
添加到属性参数哈希。
日志中有一些问题,所以如果你认为可能导致问题的东西混乱,那就问一下(我养成了让人们知道随机添加部分无法看到的坏习惯)。
答案 0 :(得分:1)
$result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
每当您创建代码时,都需要确保它是indented properly。
此代码很好;可能的问题是您正在使用来自其他视图/操作的过时代码。如果您发布了整个控制台日志以获取请求,我们可以为您提供更合适的答案。
<%= 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 %>
这是你的参数。这看起来不像"guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"smok"
;也许它可以帮助你识别正在使用的视图。
答案 1 :(得分:0)
经过几天的高低搜索后,我设法发现罪魁祸首是.coffee文件中的一些咖啡脚本编码。这个coffescript使用jquery来改变表单html在页面上的排序方式,使得表单html的部分全部混乱。 coffeescript不是必要的(我只是玩弄jquery并且忘了在一段时间后摆脱编码)所以我删除了它,瞧,问题解决了。