Friday, February 2, 2018

Rails bootstrap erb collection_check_boxes not selecting?

Leave a Comment

I have the model Game that represents games. Each game can have related games, so I have another model Related Games so Game.related_games holds the related games for that game.

The related_games model structure is [game_id, name] where game_id is the game's id the relation is linked to, and name is the name of the game that is related to that game.

Now when I display the erb like this:

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

The related games will not be selected, like its empty, while the game has related games.

Why does it happen?

4 Answers

Answers 1

I think your models are incorrect (or I did not understand your use case). Your RelatedGame model has a game_id and a name. What does this name represent? If this name is the one for the game_id, it is unnecessary (you have the name through the link game_id). If it is the name of another game, you really should have another game_id.

I believe your models should be:

Model Similarity (or another name, this is your RelatedGame, with some changes)

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

Model Game

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 

Games Controller

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 

In the view:

<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 %> 

Answers 2

You should use :related_game_ids instead of :related_games.

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

Don't forget to permit :related_game_ids in your controller.

Answers 3

I don't know what is your model look like if your relations ok and model formatted properly then

Try the following

<%= 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 %> 

and the controller strong parameters use like below

game_ids: [] 

Hope it helps

Answers 4

Did you put in your controller that related_games accepts a hash of ids?

game_controller.rb

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

in you form

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 %> 

in your model game.rb did you put the association? Something like this:

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 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment