I created a migration:

class AddNumberToGame < ActiveRecord::Migration def change add_column :games, :number, :integer, default: 0 end end 

Made changes to routes.rb:

 Rails.application.routes.draw do resources :games do get :number_lost, on: :collection get :number_win, on: :collection end end 

games_controller.rb:

 class GamesController < ApplicationController before_action :set_game, only: [:show, :edit, :update, :destroy, :number_lost, :number_win] def index @a = Array(1..25) @d = number_nil @games = Game.all end def number_lost @game.increment!(:number) redirect_to action: :index end def number_win @game.increment!(:number) redirect_to action: :index end private # Use callbacks to share common setup or constraints between actions. def set_game @game = Game.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def game_params params[:game] end def number_nil mas = Array(1..25) mas.sort_by{rand} end end 

index.html.erb:

  <div class="container"> <% @d.each do |i| %> <% if i == 12 %> <div class="count_win"><h1><%= link_to 'Win' %></h1></div> <% else %> <div class="count_lost"><h1><%= link_to 'Lost', number_lost_games_url(i) %> </h1></div> <% end %> <% end %> </div> <p>Неправельний ответ : <%= @game.number %></p> 

I get an error:

 ActionView::Template::Error (undefined method `number' for nil:NilClass): 19: <% end %> 20: </div> 21: 22: <p>Неправельний ответ : <%= @game.number %></p> 23: 24: <% @games.each do |game| %> 25: <tr> app/views/games/index.html.erb:22:in `_app_views_games_index_html_erb__510322269_48762876' 

Tell me where is my mistake?

    3 answers 3

    Here is the action code #index:

      def index @a = Array(1..25) @d = number_nil @games = Game.all end 

    and in it you select all the games and assign the variable @games. And in the submission you are referring to a single game, i.e. @game I think you should enumerate in the submission all the games and count the klaci for each one.

     <%- @games.each do |game| %> ... <p>Неправельный ответ : <%= game.number %></p> ... <%- end %> 
    • Thanks for the help, but I found a mistake) - Vitalik Andrysha

    At the time of withdrawal, you do not have the @game variable

      Error hit here:

        def game_params params[:game] end 

      I changed and everything worked:

        def game_params params.require(:game).permit(:number) end 

      index.html.erb

        <div class="container"> <% @d.each do |i| %> <% if i == 12 %> <div class="count_win"><h1><%= link_to 'Win', number_win_game_path(1) %></h1></div> <% else %> <div class="count_lost"><h1> <%= link_to 'Lost', number_lost_game_path(1) %></h1></div> <% end %> <% end %> </div> <p>Неправельний ответ : <%= @games.first.number %> </p>