i'm a bit lost and also new to rails and i couldn't find an answer to this. I think it's just my lack of knowledges in rails too.
I got Users , they own games (the opposite is true too) and a join table between them to make the link
Table(Game_User) : (user_id) & (game_id).
A user has many games
A game has many users
I would like to update my form and save the information when the user signed in and add his favorite game among a list (F.select_collection in the form).
I just dont know if the information in the join table was recorded, i dont think so, my tests in the rails console where not concluding. I'm learning by doing but sometimes it's a bit difficult :)
If I missed something just ask me ( I don't have a controller for the Table Join , is it ok ? )
Thanks a lot in advance !!
user.rb
class User < ActiveRecord::Base
has_many :game, :through => :game_user
has_many :game_user
game.rb
class Game < ActiveRecord::Base
has_many :user, :through => :game_user
has_many :game_user
end
game_user.rb
class GameUser < ActiveRecord::Base
belongs_to :game
belongs_to :user
end
games_controller.rb
before_action :set_game, only: [:show, :edit, :update, :destroy]
def index
@games = Game.all
end
def show
end
def new
@game = Game.new
end
def edit
end
def create
@game = Game.new(game_params)
respond_to do |format|
if @game.save
format.html { redirect_to @game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @game.update(game_params)
format.html { redirect_to @game, notice: 'Game was successfully updated.' }
format.json { render :show, status: :ok, location: @game }
else
format.html { render :edit }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
def destroy
@game.destroy
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_game
@game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:name, :plateform, :plateform_id, :typegame)
end
end
users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
end
Last item , the form for editing the profile of the person (with Devise gem btw)
extract of the form
<div class="panel-title-edit">
<h1>Edit <%= resource_name.to_s.humanize %></h1>
</div>
</div>
<div class="panel-body-edit">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :country %><br />
<%= f.text_field :country, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :game %><br />
<%= f.collection_select(:game, Game.all, :id, :name, {:multiple => true}, class: "form-control") %>
</div>
<div class="form-group">
<%= f.submit "Update", class: "btn btn-primary" %>
</div>
<% end %>
Aucun commentaire:
Enregistrer un commentaire