Tôi gặp phải lỗi ActiveSupport::HashWithIndifferentAccess
khi tôi cố gắng cập nhật biểu mẫu được nhúng.ActiveSupport :: HashWithIndifferentAccess trên Cập nhật Biểu mẫu Nhúng
Dưới đây là ví dụ đơn giản:
Mẫu:
<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 %>
Models:
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
Bộ điều khiển:
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
Đây là đối tượng params[:players_to_team]
khi hình thức s ubmission:
không một lỗi ActiveSupport::HashWithIndifferentAccess
nghĩa là gì? Tôi cần phải làm gì để có biểu mẫu này cập nhật mục nhập players_to_team
?
Sửa
BT
là một cột trong players_to_teams
. Nếu tôi xóa teh field_for
khối, tôi có thể lưu thành công trường BT
/hàng players_to_teams
.
Cảm ơn
những gì đang thuộc tính "BT" - đó là tên chính xác của một trường trên bảng players_to_teams? –
Yea. Đã cập nhật câu hỏi để cung cấp thêm thông tin. –
bạn có thể thay đổi "<% = field.fields_for @ players_to_teams.player" thành "<% = field.fields_for: player" –