I am pretty new to Ruby and I am working on a hangman game .
What I am trying to do is create a new game when the user simply click on a button and I want that "click" to redirect to the show of that game.
Here are my models :
class Game < ApplicationRecord
has_many :guesses
end
class Guess < ApplicationRecord
belongs_to :game
end
Here are my controllers :
class GamesController < ApplicationController
skip_before_action :authenticate_user!
def index
@games = Game.all
end
def create
@game = Game.new(game_params)
@game.save!
redirect_to game_path(@game)
end
def show
@game = Game.new
@game = Game.last
end
def destroy
@game = Game.find(params[:id])
@game.destroy
redirect_to home_path
end
private
words =[
"spokesperson", "firefighter", "headquarters", "confession", "difficulty", "attachment", "mechanical",
"accumulation", "hypothesis", "systematic", "attraction", "distribute", "dependence", "environment",
"jurisdiction", "demonstrator", "constitution", "constraint", "consumption", "presidency", "incredible",
"miscarriage", "foundation", "photography", "constituency", "experienced", "background", "obligation",
"diplomatic", "discrimination", "entertainment", "grandmother", "girlfriend", "conversation", "convulsion",
"constellation", "leadership", "insistence", "projection", "transparent", "researcher", "reasonable","continental",
"excavation", "opposition", "interactive", "pedestrian", "announcement", "charismatic", "strikebreaker",
"resolution", "professional", "commemorate", "disability", "collection", "cooperation", "embarrassment",
"contradiction", "unpleasant", "retirement", "conscience", "satisfaction", "acquaintance", "expression",
"difference", "unfortunate", "accountant", "information", "fastidious", "conglomerate", "shareholder",
"accessible", "advertising", "battlefield", "laboratory", "manufacturer", "acquisition", "operational",
"expenditure", "fashionable", "allocation", "complication", "censorship", "population", "withdrawal",
"sensitivity", "exaggerate", "transmission", "philosophy", "memorandum", "superintendent", "responsibility",
"extraterrestrial", "hypothesize", "ghostwriter", "representative", "rehabilitation", "disappointment",
"understanding", "supplementary", "preoccupation"
]
@word_to_guess = words.sample
@health_bar = 5
@game_status = "Game not started yet"
def game_params
params.require(:game).permit(@word_to_guess, @health_bar, @game_status)
end
end
Here is what I have been trying to do
<%= link_to "nouvelle partie", game_path(game), method: :create %>
but the errors message is : "undefined local variable or method `game' for #ActionView::Base:0x0000000000d4d0"
CodePudding user response:
POST method won't work with link_to. Try using the to button_to
instead of link_to
like this:
<%= button_to "nouvelle partie", game_path(game), method: :post %>
I think the bug is due to the variable game not being passed to partial. You have to pass it on like this:
<%= render "game", game: @game %>
And you don't have to pass variables in game_params
. You only need to set them in the show
method where you will be redirected from the create
method. Also, you don't set the current game like this Game.last
, you just take id
which is contained in the url.
def create
@game = Game.new
@game.save!
redirect_to game_path(@game)
end
def show
@game = Game.find(params[:id])
@word_to_guess = words.sample
@health_bar = 5
@game_status = "Game not started yet"
end
CodePudding user response:
have you tried?
<%= link_to "nouvelle partie", game_path(@game), method: :create %>