【问题标题】:Rails bootstrap erb collection_check_boxes not selecting?Rails bootstrap erb collection_check_boxes没有选择?
【发布时间】:2017-12-20 22:09:39
【问题描述】:

我有代表游戏的模型Game。每个游戏都可以有相关的游戏,所以我有另一个模型Related Games 所以Game.related_games 持有该游戏的相关游戏。

related_games 模型结构是[game_id, name],其中game_id 是关系所链接到的游戏ID,name 是与该游戏相关的游戏名称。

现在当我这样显示 erb 时:

f.collection_check_boxes :related_games, Game.all, :id, :name

相关游戏不会被选中,如为空,而游戏有相关游戏。

为什么会这样?

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap


    【解决方案1】:

    我认为您的模型不正确(或者我不理解您的用例)。您的 RelatedGame 模型有一个 game_id 和一个名称。这个名字代表什么?如果此名称是 game_id 的名称,则不需要(您通过链接 game_id 获得名称)。如果它是另一个游戏的名称,你真的应该有另一个game_id。

    我相信你的模型应该是:

    模型相似度(或其他名称,这是您的相关游戏,有一些变化)

    class Similarity < ApplicationRecord
      belongs_to :game1, :foreign_key => 'game1_id', :class_name => "Game"
      belongs_to :game2, :foreign_key => 'game2_id', :class_name => "Game"
    end
    

    模型游戏

    class Game < ApplicationRecord
      has_many :similarities1, :class_name => "Similarity", :foreign_key => "game1_id"
      has_many :similarities2, :class_name => "Similarity", :foreign_key => "game2_id"
    
      has_many :similar_games, :through => :similarities1, source: "Game"
      has_many :inverse_similar_games, :through => :similarities2, source: "Game"
    
      def all_similar_games
        (similar_games + inverse_similar_games).uniq
      end
    end
    

    游戏控制器

    def edit
      # The game to edit
      @game = Game.find_by(id: params[:id])
    
      # The relation table (similarity) where this game is the game1
      @related = @game.similarities1
    
      # The games related to this game (all games linked by game2 in the similarities relation, where this game is game1)
      @similar_games = @game.similar_games
    
      # All games related to this one through similarity, whether through game1 or game2
      @all_similar_games = @game.all_similar_games
    end
    

    在视图中:

    <h1>@game.name</h1>
    <% form_for @game do |f| %>
      <%= f.input_field :name %>
      <%= f.collection_check_boxes :similar_game_ids, Game.all, :id, :name) %>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      您应该使用:related_game_ids 而不是:related_games

      f.collection_check_boxes :related_game_ids, Game.all, :id, :name

      别忘了在你的控制器中允许:related_game_ids

      【讨论】:

        【解决方案3】:

        如果您的关系正常并且模型格式正确,我不知道您的模型是什么样的

        试试下面的

        <%= f.collection_check_boxes :game_ids, Game.all, :id, :name do |cb| %>
            <% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text } %>
        <% end %>
        

        控制器strong parameters 使用如下

        game_ids: []
        

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          您是否在控制器中设置了 related_games 接受 id 散列?

          game_controller.rb

          private
          def game_params
                params.require(:game).permit(
                    //fields_of_game,               
                    related_game_ids: []
                  )
              end
          

          在你的形式

          games/_form.html.erb

           <%= hidden_field_tag "game[related_game_ids][]", nil %>
                  <% Related_Games.all.each do |r_games| %>
          
                        <%= check_box_tag "game[related_game_ids][]", r_games.id, @game.related_game_id.include?(r_game.id), id: dom_id(r_game) %>
                        <%= label_tag dom_id(r_game), r_game.name %> <br>
          
                  <% end %>
          

          在你的模型中game.rb 你把协会了吗? 像这样的:

          accepts_nested_attributes_for :related_games
          def related_games_attributes=(related_game_attributes)
              related_game_attributes.values.each do |related_game_attribute|
                related_game = Related_Games.find_or_create_by(related_game)
                self.related_game << related_game 
              end
            end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多