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?