加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 嵌入式表单更新上的ActiveSupport :: HashWith

发布时间:2020-12-17 03:14:10 所属栏目:百科 来源:网络整理
导读:我尝试更新嵌入的表单时收到ActiveSupport :: HashWithIndifferentAccess错误. 这是最简单的例子: 形成: h1PlayersToTeams#edit/h1%= form_for @players_to_teams do |field| % %= field.fields_for @players_to_teams.player do |f| % %= f.label :IsActi
我尝试更新嵌入的表单时收到ActiveSupport :: HashWithIndifferentAccess错误.

这是最简单的例子:

形成:

<h1>PlayersToTeams#edit</h1>
<%= form_for @players_to_teams do |field| %>
    <%= field.fields_for @players_to_teams.player do |f| %>
        <%= f.label :IsActive %>
        <%= f.text_field :IsActive %>
    <% end %>
    <%= field.label :BT %>
    <%= field.text_field :BT %>
    <br/>
    <%= field.submit "Save",class: 'btn btn-primary' %>
<% end %>

楷模:

class PlayersToTeam < ActiveRecord::Base
  belongs_to :player
  belongs_to :team

  accepts_nested_attributes_for :player
end

class Player < ActiveRecord::Base
  has_many :players_to_teams
  has_many :teams,through: :players_to_teams
end

控制器:

class PlayersToTeamsController < ApplicationController
  def edit
    @players_to_teams=PlayersToTeam.find(params[:id])
  end

  def update
    @players_to_teams=PlayersToTeam.find(params[:id])
    respond_to do |format|
      if @players_to_teams.update_attributes(params[:players_to_team])
        format.html { redirect_to @players_to_teams,notice: 'Player_to_Team was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @players_to_teams.errors,status: :unprocessable_entity }
      end
    end
  end
end

这是表单提交时的params [:players_to_team]对象:

?

ActiveSupport :: HashWithIndifferentAccess错误是什么意思?要让此表单更新players_to_team条目,我需要做些什么?

编辑

BT是players_to_teams中的一列.如果我删除了teh field_for块,我可以成功保存BT字段/ players_to_teams行.

谢谢

解决方法

归功于@Brandan.回答: What is the difference between using “:” and “@” in fields_for

Okay,I was able to clone your sample project and reproduce the error.
I think I understand what’s happening.

After your call to accepts_nested_attributes_for,you now have an
instance method on your model named player_attributes=. This is in
addition to the player= method that’s normally defined for a has_one
association. The player_attributes= method accepts a hash of
attributes,whereas the player= method only accepts an actual Player
object.

Here’s an example of the text input generated when you called
fields_for @players_to_teams.player:

and here’s that
same input when calling fields_for :player:

When you
call update_attributes in your controller,the first example will call
player=,while the second example will call player_attributes=. In
both cases,the argument passed to the method is a hash (because
params is ultimately just a hash of hashes).

That’s why you were getting an AssociationTypeMismatch: you can’t pass
a hash to player=,only a Player object.

It appears that the only safe way to use fields_for with
accepts_nested_attributes_for is by passing the name of the
association and not the association itself.

So to answer your original question,the difference is that one works and the other doesn’t

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读