Ruby on rails 为什么我在游戏中得到了命名错误?对于我的游戏形式来说是新的,但是对于我的用户来说是模拟的?新的
Ruby on rails 为什么我在游戏中得到了命名错误?对于我的游戏形式来说是新的,但是对于我的用户来说是模拟的?新的,ruby-on-rails,nomethoderror,Ruby On Rails,Nomethoderror,我正在RoR中创建一个a站点,并且我已经建立了用户注册和登录表单。一切都很好。问题是,我去创建了另一个名为games的对象,它的功能与用户几乎相同,但是当我尝试与它交互时,我得到了一个错误。我建立的形式几乎完全一样,我的路由全等 以下是我的用户new.html.erb: <!DOCTYPE html> <html> <body> <% provide(:title, 'Sign up') %> <h1
我正在RoR中创建一个a站点,并且我已经建立了用户注册和登录表单。一切都很好。问题是,我去创建了另一个名为games的对象,它的功能与用户几乎相同,但是当我尝试与它交互时,我得到了一个错误。我建立的形式几乎完全一样,我的路由全等 以下是我的用户new.html.erb:
<!DOCTYPE html>
<html>
<body>
<% provide(:title, 'Sign up') %>
<h1 class="heading1" >Sign up</h1>
<br>
<div>
<%= form_for(@user, :html => { :class => 'form' }) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :email %>
<%= f.text_field :email %>
<br>
<%= f.label :username %>
<%= f.text_field :username %>
<br>
<%= f.label :password %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<br>
<%= f.submit "Create my account", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(@game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
<br>
<%= i.submit "Create Game", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
还有我的游戏new.html.erb:
<!DOCTYPE html>
<html>
<body>
<% provide(:title, 'Sign up') %>
<h1 class="heading1" >Sign up</h1>
<br>
<div>
<%= form_for(@user, :html => { :class => 'form' }) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :email %>
<%= f.text_field :email %>
<br>
<%= f.label :username %>
<%= f.text_field :username %>
<br>
<%= f.label :password %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<br>
<%= f.submit "Create my account", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(@game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
<br>
<%= i.submit "Create Game", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
和我的路由文件:
Rails.application.routes.draw do
resources :sessions, only: [:new, :create, :destroy]
resources :users
match '/new_game', to: 'games#new', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#home'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
rails服务器错误页面显示:
NoMethodError in Games#new
Showing /Users/Karen/Desktop/BR2/app/views/games/new.html.erb where line #7 raised:
undefined method `games_path' for #<#<Class:0x007fbfd6bdb260>:0x007fbfd6bd8948>
Extracted source (around line #7):
4
5
6
7
8
9
10
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(@game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
Rails.root: /Users/Karen/Desktop/BR2
Application Trace | Framework Trace | Full Trace
app/views/games/new.html.erb:7:in `_app_views_games_new_html_erb___3427370169918602482_70230959128880'
Request
Parameters:
None
NoMethodError在游戏中#新增
显示/Users/Karen/Desktop/BR2/app/views/games/new.html.erb,其中第7行出现:
未定义的方法“游戏路径”#
提取的源(第7行附近):
4.
5.
6.
7.
8.
9
10
创造游戏
{:class=>'form'})do | i |%>
Rails.root:/Users/Karen/Desktop/BR2
应用程序跟踪|框架跟踪|完整跟踪
app/views/games/new.html.erb:7:in`_app_views_games_new_html_erb_3427370169918602482_70230959128880'
要求
参数:
没有一个
我真的很感激所有的帮助。如果有任何更多的信息,我可以提供,请这样说
谢谢您您会发现,当您在控制台中执行
bundle exec rake routes
时,您实际上并没有为游戏路径创建命名路由
如果您正在使用match
并想命名一条路线(因此您有类似games\u path
的东西可用),您必须执行以下操作:
match `/games`, as: 'games', to: 'games#index', via: :get
一种更简单的方法是为大多数路由使用资源,只需使用默认的RESTFUL路径:
resources :games
# Now you have access to '/games/new', '/games/:id',
# '/games', etc, as well as names such as `games_path`.
# Check `bundle exec rake routes` for all of them.
有关更多信息,请参阅
最好的选择是在
routes.rb
resources :games
并通过“获取”路径将match'/new#game'删除到:“games#new”
这样做将为您提供以下Restful路线:
Prefix Verb URI Pattern Controller#Action
games GET /games(.:format) games#index
POST /games(.:format) games#create
new_game GET /games/new(.:format) games#new
edit_game GET /games/:id/edit(.:format) games#edit
game GET /games/:id(.:format) games#show
PATCH /games/:id(.:format) games#update
PUT /games/:id(.:format) games#update
DELETE /games/:id(.:format) games#destroy
因此,在提交表单时,您的应用程序将通过HTTP Post请求路由到创建
操作(游戏路径
)
目前,您刚刚为游戏
资源定义了一条路线,该路线通过
match '/new_game', to: 'games#new', via: 'get'
但是没有创建
操作的路径,这就是为什么您在表单上收到错误为未定义的方法“games\u path”
如果您不希望使用RESTful路由(resources:games
),则必须将路由定义为:
match '/games', as: 'games', to: 'games#create', via: 'post'
对于创建
操作。rout helper错误后的标准第一步是rake routes,以查看您从路由配置中真正获得的帮助。非常感谢。我觉得自己像个白痴。我想我只是忘了将它声明为资源。再次感谢您的帮助。如果您发现我的答案解决了您的问题,请随意接受;)