Reputation: 141
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.
Upvotes: 0
Views: 153
Reputation: 3568
As mentioned by Pavan, you can force the path to be defined your way using as: :path_name
.
Before that, I would have a look at my routes to make sure everything is defined to begin with and that you don't have unnecessary routes.
If you don't need to communicate with your server in a RESTful way, then perhaps it's a bad idea to use resources
since this will lead to internal errors.
You can check your routes using rake routes
or while in development you can go to localhost:port/rails/info/routes
.
At first glance, I would suspect that your path is actually play_game1_path(@game1)
, with the action first.
Upvotes: 2
Reputation: 10124
You are using resources :game1
which gives you a seven methods: index, new, create, show, edit, update, destroy. You should only specify the ones you want to use like
resources :game1, only: [:index]
You can also make things more organized with nested routes by
resources :game1, only: [:index] do
get "instruction"
end
resources :pages, only: [:show] do
get "about"
end
With this setup, you will need a PagesController. The default param for show routes is :id
Resource: http://guides.rubyonrails.org/routing.html
Upvotes: 0