gpt4 book ai didi

ruby-on-rails - 给定索引值,我如何才能只更新 Rails Controller 中的那些值?

转载 作者:行者123 更新时间:2023-12-04 07:30:28 25 4
gpt4 key购买 nike

我有一个嵌套表单,如下所述,带有一个复选框,我希望用它来仅记录我想更新的记录。我已经检查了 html,并且正在为每条记录注册正确的索引。示例属性和复选框的 html 如下所示:

<input id="game_game_rosters_attributes_0_game_id" name="game[game_rosters_attributes][0][game_id]" type="hidden" value="127">
<input id="add_0" name="add[0]" type="checkbox" value="false">

现在,我认为我应该通过检查具有相同索引的复选框是否具有值“来确定我想更新哪个game_roster_attributes真的”。但我不确定如何在我的 games_controller.rb 中执行此操作,因为它当前设置为使用 @game.update_attributes()...

批量分配我的 attr_accessible 变量

games_controller.rb

def update
@game = Game.find(params[:id])

respond_to do |format|
if @game.update_attributes(params[:game])
format.html { redirect_to @game, notice: 'Game was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit"}
format.json { render json: @game.errors, status: :unprocessable_entry }
end
end
end

嵌套形式

<%= form_for @game do |f| %>

# @game fields for editing here...

<% roster_options.each.with_index do |option, index| %>
<%= f.fields_for :game_rosters, option do |fields| %>
<%= fields.object.player.full_name %>
<%= fields.hidden_field :player_id %>
<%= fields.hidden_field :game_id %>
<%= check_box_tag 'add[' + index.to_s + ']', false %>
<% end %>
<% end %>

<% end %>

模型

game.rb

class Game < ActiveRecord::Base
attr_accessible :date, :game_rosters_attributes
has_many :game_rosters
has_many :players, :through => :game_rosters
accepts_nested_attributes_for :game_rosters, allow_destroy: true
end

game_roster.rb

class GameRoster < ActiveRecord::Base
attr_accessible :active, :winner, :player_id, :game_id, :placement
belongs_to :game
belongs_to :player
end

game_controller.rb

def new
@game = Game.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @game }
end
end

def edit
@game = Game.find(params[:id])
@game_roster = GameRoster.joins(:player).where(game_id: params[:id]).order('winner DESC, placement DESC')
@options = SeasonRoster.where("season_id = (?) AND NOT EXISTS ( SELECT * FROM game_rosters INNER JOIN games ON games.id = game_rosters.game_id WHERE game_rosters.player_id = season_rosters.player_id AND games.id = (?))", @game.season_id.to_s, @game.id.to_s)

@roster_options = Array.new
@options.each do |o|
c = GameRoster.new
c.player_id = o.player_id
c.challenge_id = params[:id]
c.winner = false
@roster_options.push(c)
end

end

最佳答案

我不确定你的要求。我假设您只想保存那些选中了“添加”字段的花名册,并删除其余的花名册。

roster.rb

class Roster < ActiveRecord::Base
attr_accessor :add
end

游戏.rb

class Game < ActiveRecord::Base
before_save :mark_rosters_for_removal

def mark_rosters_for_removal
rosters.each do |roster|
unless roster.add
roster.mark_for_destruction
end
end
end
end

嵌套形式

<%= form_for @game do |f| %>

# @game fields for editing here...

<% roster_options.each.with_index do |option, index| %>
<%= f.fields_for :game_rosters, option do |fields| %>
<%= fields.object.player.full_name %>
<%= fields.hidden_field :player_id %>
<%= fields.hidden_field :game_id %>
<%= fields.check_box :add, false %>
<% end %>
<% end %>

<% end %>

关于ruby-on-rails - 给定索引值,我如何才能只更新 Rails Controller 中的那些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com