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
enddef number_win
@game.increment!(:number)
redirect_to action: :index
endprivate
Use callbacks to share common setup or constraints between actions.
def set_game
@game = Game.find(params[:id])
endNever trust parameters from the scary internet, only allow the white list through.
def game_params
params[:game]
enddef 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've got a mistake:
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 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 %>