简体   繁体   English

根据下拉菜单中的选择动态初始化未保存的父对象的子记录

[英]Dynamically initialize child records for unsaved parent object based on a selection in dropdown

I have a problem which I feel shouldn't be as complex as it feels to me at the moment. 我有一个问题,我觉得这应该不像现在这样复杂。 I want to initialize child records based on a selection made on a dropdown in the parent form and then show these childs (at the moment always 2) for further filling. 我想基于在父表单中的下拉列表上进行的选择来初始化子记录,然后显示这些子记录(此刻始终为2)以进行进一步填充。

There are several mostly relevant answers here and elsewhere around the web, but so far I have failed in building a working solution. 在这里和网络的其他地方,有几个最相关的答案,但是到目前为止,我还没有建立一个可行的解决方案。

Main building blocks are as follows. 主要组成部分如下。

game.rb (parent) game.rb (父级)

class Game < ApplicationRecord
  has_many :game_players, dependent: :destroy
  belongs_to :scenario

  accepts_nested_attributes_for :game_players
end

game_player.rb (children) game_player.rb (儿童)

class GamePlayer < ApplicationRecord
  belongs_to :game
  belongs_to :user
  belongs_to :force
end

In a form for new game I have following dropdown 在新游戏的表格中,我有以下下拉列表

<div class="form-group">
  <%= form.label :scenario_id %>
  <%= form.collection_select :scenario_id, Scenario.all, :id, :name, {},     { class: "form-control" }   %>
</div>

Selection from this dropdown filters the possible forces for game_players (each scenario has two opposing forces). 从此下拉列表中进行选择可以过滤game_players的可能力量(每种情况都有两个相反的力量)。

I grab the scenario_id in game.coffee 我在game.coffee获取了game.coffee

jQuery ->
  scenarios = $('[name*=scenario_id]').html()
  $('[name*=scenario_id]').change ->
    scenario_id = $('[name*=scenario_id] :selected').val()
    params = 'scenario_id=' + scenario_id
    $.ajax {
      url: "/games/prepare_players",
      data: params
      success: () ->
    }

And here I am stuck. 在这里我被困住了。 Based on what I have read I am trying to fire up this action in games_controller.rb . 根据我阅读的内容,我正在尝试在games_controller.rb启动此操作。

def prepare_players
  forces = ScenarioForce.where(scenario_id: params[:scenario_id])
  forces.each do |force|
    @game.game_players << GamePlayer.new( force_id: force.id)
  end
  respond_to do |format|
    format.js
  end
end

Which again should render this in prepare_players.js.erb 哪个应该再次在prepare_players.js.erb呈现

$('[name=player-cards]').html("<%= escape_javascript(render partial: 'players', locals: {form: form}).html_safe %>")

Which should show the form for both players where it is possible to choose player from users for both sides. 这应该显示两个玩家的表格,可以从双方用户中选择玩家。

<div name="player-cards">
  <h3>Players</h3>
  <% @game.game_players.each do |player| %>
    <p><%= player.force.name %></p>
    <%= form.fields_for player do |pf| %>
      <div class="form-group">
        <%= pf.label :name %>
        <%= pf.collection_select :user_id, User.all, :id, :full_name, {},     {class: "form-control"}  %>
      </div>
    <% end %>
  <% end %>
</div>

But as I mentiond I am stuck in the Ajax-call. 但是正如我提到的那样,我陷入了Ajax调用中。 My controller action is not fired and instead I get show-action with id=prepare_players. 我的控制器操作未触发,而是使用id = prepare_players获得显示操作。 In routes.rb I have this routes.rb我有这个

get 'games/prepare_players', to: 'games#prepare_players'

UPDATE As per Sebastiáns advice I am now hitting the correct action in games_controller. 更新根据塞巴斯蒂安的建议,我现在在games_controller中执行了正确的操作。 But as I feared, I can't add new game_players to my parent without knowing relevant id. 但正如我担心的那样,如果不知道相关的ID,就无法将新的game_players添加到我的父母中。 It would be easy to pass the id if I just had one, but I don't because parent has not been saved yet. 如果我只有一个,则传递ID很容易,但是我没有,因为尚未保存父代。

I am using Rails 5.1.4 at the moment. 我目前正在使用Rails 5.1.4。

Any pointers guiding me towards getting this to work are highly appreciated. 任何指导我朝着使它起作用的指示都受到高度赞赏。

Make sure the route you're using for the AJAX request is being defined before any other, for instance a resource :games . 确保先定义用于AJAX请求的路由,然后定义其他路由,例如resource :games If the route is after this one, then it'll fire the set_game method and redirect you to games#show, and there you'll receive an error like: 如果路线在此路线之后,则将触发set_game方法并将您重定向到games#show,然后您将收到类似以下错误:

Completed 404 Not Found in 4ms (ActiveRecord: 0.3ms) 在4毫秒内完成404找不到(ActiveRecord:0.3毫秒)

ActiveRecord::RecordNotFound (Couldn't find Game with 'id'=prepare_players): ActiveRecord :: RecordNotFound(找不到带有'id'= prepare_players的游戏):

app/controllers/games_controller.rb:65:in `set_game' app / controllers / games_controller.rb:65:在`set_game'中

Note the games_controller.rb:65:in set_game'`. 注意games_controller.rb:65:in :。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript - 如何设置子下拉菜单基于父下拉菜单中选择 - Javascript - how to set child drop down menu based on parent dropdown menu selection 根据第一个下拉列表中的选择动态填充3个下拉列表 - Dynamically populate 3 dropdowns based on the selection in first dropdown 如何基于另一个下拉选择动态填充的下拉列表中选择? - How to dynamically populate options on dropdown lists based on selection in another dropdown? Javascript根据下拉选择获取自定义对象 - Javascript get custom object based on dropdown selection 将子组件文本字段的值设置为父组件下拉选择? - Set Child Component TextField's Value to Parent Components Dropdown Selection? 动态创建嵌套的 JSON 对象父子对象 - Create a nested JSON object dynamically parent and child 根据选择的一个下拉列表动态填充下拉列表 - dynamically populating a drop down based on selection of one dropdown 根据php中下拉列表的选择,动态填充文本框中的db值 - Populate db values fill dynamically in textbox, based on selection of dropdown in php 根据下拉选择动态填充字段(Javascript / HTML) - Dynamically populate a field based on dropdown selection (Javascript / HTML) 动态添加下拉列表,并基于一个下拉列表中的选择添加其他列表中的选项 - Adding dropdowns dynamically and based on selection in one dropdown select options in other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM