dimanche 22 mai 2016

undefined method `_path' when using params

I'm new to Rails and currently I'm building a game app. My game contains multiple levels. I want the url of the game to contain the number of each level. For example:

http://localhost:3000/game1/play/1
http://localhost:3000/game1/play/2

In order to achieve this, I understand that I need to use params, here's my code:

routes.rb:

Rails.application.routes.draw do

  devise_for :users

  resources :game1

  get "/game1" => "game1#index"
  get "/game1/play/:level" => "game1#play"
  get "/game1/instruction" => "game1#instruction"


  get "/pages/*page" => "pages#show"
  get "/pages/about" => "pages#about"

  root "pages#show", page: "home"

end

controller:

class Game1Controller < ApplicationController

    def index
    end

    def play

        @game1 = Game1lv.find(params[:level])

        @userid = current_user.id
        @usergame1lv = User.where(id: @userid).limit(1).pluck(:game1lv) 
        if @usergame1lv == [nil]
            @usergame1lv = 1
        end

        @game1l = Game1lv.where(:level => @usergame1lv).limit(1).pluck(:imagelink)
        @game1a = Game1lv.where(:level => @usergame1lv).limit(1).pluck(:answer)
        @game1link = @game1l[0].to_s
        @game1answer = @game1a[0].to_s

        @game1answer_user = params["answer"]

        if @game1answer_user == @game1answer
            redirect_to game1_play_path(@game1), :flash => { :success => "You are right!" }
        else
            #flash.now[:alert] = 'You are wrong! Lets try again!'
        end

    end

    def instruction
    end

end

view:

<body><center>

    <br><b>Answer: </b><br><br>

    <%= form_tag game1_play_path(@game1), :method => :get, :id => "text_form" do %>

        <%= text_field_tag "answer", "" ,class: 'textbox_game' %>
        <br><br>
        <%= submit_tag("Submit", :class => "button_game") %>

    <% end %>


</center></body>

Now when I go to the url:

http://localhost:3000/game1/play/1

Rails show the error:

undefined method `game1_play_path' for #<#<Class:0x943de50>:0x9447720>

Rails indicate that the error is at this line in the view file:

<%= form_tag game1_play_path(@game1), :method => :get, :id => "text_form" do %>

Please show me what I'm doing wrong and why that method is undefined. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire