How do you count the number of crossings on the reference? There's a mistake "undefined method `number' for nil:NilClass."



  • I created a migration:

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

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

    &lt;p&gt;Неправельний ответ : &lt;%= @game.number %&gt;&lt;/p&gt;
    

    I've got a mistake:

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

    Tell me where my mistake is.



  • Here's the action code.

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

    end

    and in it, you choose all the games and assign the variable @games.
    And in your presentation, you're talking about one game, that is, @game. I think you should list all the games for each and every single one of them as claws.

    <%- @games.each do |game| %>
    ...
    <p>Неправельный ответ : <%= game.number %></p>
    ...
    <%- end %>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2