我创建了一个应用程序,它基本上是具有交互式和动态形式的RPG的角色创建者。 我使用Rails 5.0.0.1,我无法正确更新我的表单。 基本模型更新,但所有嵌套都没有。
所以我有
class Character < ApplicationRecord has_many :attr4characters, autosave: true accepts_nested_attributes_for :attr4characters, allow_destroy: true end和
class Attr4character < ApplicationRecord belongs_to :character end它代表一个角色和一组他的属性。 Attr4character中的每条记录都是不同的属性。
Show视图很简单:
... <div class="field"> <%= f.label :description %> <%= f.text_area :description %> </div> <div class="field"> <%= f.label :character_type %> <%= f.select(:character_type, options_for_select(["Zhong Lung", "Shih", "Hsien", "Garou", "Technocrat"])) %> </div> <% f.object.attr4characters.each do |attr| %> <%= f.fields_for attr do |attr_f| %> <div class="field"> <%= attr_f.label "Field name" %> <%= attr_f.text_field :field_name %> </div> <div class="field"> <%= attr_f.label "Field value" %> <%= attr_f.text_field :field_value %> </div> <% end %> <% end %> <div class="actions"> <%= f.submit %> </div> ...最后我的characters_controller:
def update respond_to do |format| if @character.update(character_params) format.html { redirect_to @character, notice: 'Character was successfully updated.' } format.json { render :show, status: :ok, location: @character } else format.html { render :edit } format.json { render json: @character.errors, status: :unprocessable_entity } end end end private def set_character @character = Character.find(params[:id]) end def character_params params.require(:character).permit(:id, :name, :player, :description, :character_type, attr4characters_attributes: [:id, :character_id, :field_name, :field_value]) end所以,我有一个表单,它正确显示一个字符和他的所有属性集。 当我更新字符字段(如:description)时,它通常会更新。 当我更新任何嵌套字段时,Rails说该字符已成功更新,并且没有任何更改! 我在Google上搜索过,我发现Rails表单中的嵌套属性存在很多问题,但没有一个方法适合我。 我甚至认为这是Rails 4中的一个错误,但我使用的是第5版......
请关于这个主题的建议。 真的是个bug吗? 或者我做错了什么? 我是Rails的新手,所以我不排除这种可能性。 :)
顺便说一句,在服务器的日志中,我发现有关于attr4characters的警告。
Processing by CharactersController#update as HTML Parameters: {"utf8"=>"\u2713", "authenticity_token"=>"xeYyIRc13YiOk29v18rFM6Oh5OHRRuPpSKEQuIHE/U4uhANEF7TwMp8mb6hv6L7mUAm5MngAuyFayHcWV/Vvbw==", "character"=>{"name"=>"Ray", "player"=>"111", "description"=>"A Zhong Lung suicider", "character_type"=>"Hsien", "attr4character"=>{"field_name"=>"Gift1", "field_value"=>"Sense Wyrm"}}, "commit"=>"Update Character", "id"=>"3"} Character Load (0.2ms) SELECT "characters".* FROM "characters" WHERE "characters"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] Unpermitted parameter: attr4character (0.1ms) begin transaction SQL (0.9ms) UPDATE "characters" SET "character_type" = ?, "updated_at" = ? WHERE "characters"."id" = ? [["character_type", "Hsien"], ["updated_at", 2016-09-13 14:39:10 UTC], ["id", 3]] (24.3ms) commit transaction但是characters_controller中允许使用attr4字符...
I create an application, which is basically a character creator for an RPG with interactive and dynamic forms. I use Rails 5.0.0.1, and I cannot update my form properly. The base model updates well, but all nested don't.
So, I have
class Character < ApplicationRecord has_many :attr4characters, autosave: true accepts_nested_attributes_for :attr4characters, allow_destroy: true endand
class Attr4character < ApplicationRecord belongs_to :character endwhich represent a character and a set of his attributes. Each record in Attr4character is a different attribute.
The Show view is simple:
... <div class="field"> <%= f.label :description %> <%= f.text_area :description %> </div> <div class="field"> <%= f.label :character_type %> <%= f.select(:character_type, options_for_select(["Zhong Lung", "Shih", "Hsien", "Garou", "Technocrat"])) %> </div> <% f.object.attr4characters.each do |attr| %> <%= f.fields_for attr do |attr_f| %> <div class="field"> <%= attr_f.label "Field name" %> <%= attr_f.text_field :field_name %> </div> <div class="field"> <%= attr_f.label "Field value" %> <%= attr_f.text_field :field_value %> </div> <% end %> <% end %> <div class="actions"> <%= f.submit %> </div> ...And finally my characters_controller:
def update respond_to do |format| if @character.update(character_params) format.html { redirect_to @character, notice: 'Character was successfully updated.' } format.json { render :show, status: :ok, location: @character } else format.html { render :edit } format.json { render json: @character.errors, status: :unprocessable_entity } end end end private def set_character @character = Character.find(params[:id]) end def character_params params.require(:character).permit(:id, :name, :player, :description, :character_type, attr4characters_attributes: [:id, :character_id, :field_name, :field_value]) endSo, I have a form, which correctly display a character and all set of his attributes. When I update a character field (like :description), it is normally updated. When I update any nested field, Rails says that character is successfully updated and nothing changes! I searched with Google, and I found a lot of problems with nested attributes in Rails forms, but none of the recipes worked for me. I even encountered opinions that it is a bug in Rails 4, but I use 5th version...
Please, advice on the topic. Is it a bug really? Or am I doing something wrong? I'm new on Rails, so I don't exclude that possibility. :)
By the way, in the server's log I found that there is warning about attr4characters.
Processing by CharactersController#update as HTML Parameters: {"utf8"=>"\u2713", "authenticity_token"=>"xeYyIRc13YiOk29v18rFM6Oh5OHRRuPpSKEQuIHE/U4uhANEF7TwMp8mb6hv6L7mUAm5MngAuyFayHcWV/Vvbw==", "character"=>{"name"=>"Ray", "player"=>"111", "description"=>"A Zhong Lung suicider", "character_type"=>"Hsien", "attr4character"=>{"field_name"=>"Gift1", "field_value"=>"Sense Wyrm"}}, "commit"=>"Update Character", "id"=>"3"} Character Load (0.2ms) SELECT "characters".* FROM "characters" WHERE "characters"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] Unpermitted parameter: attr4character (0.1ms) begin transaction SQL (0.9ms) UPDATE "characters" SET "character_type" = ?, "updated_at" = ? WHERE "characters"."id" = ? [["character_type", "Hsien"], ["updated_at", 2016-09-13 14:39:10 UTC], ["id", 3]] (24.3ms) commit transactionBut attr4characters are permitted in the characters_controller...
最满意答案
警告告诉您它忽略了所有attr4character属性。 您的permit代码与您的型号一样正确,但您的视图与它们不匹配。 你应该这样做
f.fields_for :attr4characters do |attr_f| ... end让rails处理迭代关联。 这也将确保正确命名属性(因此白名单将允许它们通过)
The warning is telling you that it is ignoring all the attr4character attributes. Your permit code is correct as is your model, but your view doesn't match them. You should be doing
f.fields_for :attr4characters do |attr_f| ... endAnd let rails handle iterating over the association. This will also ensure that attributes are named correctly (so they will be allowed through by your whitelist)
更多推荐
发布评论