Ruby-on-rails Rails 路由错误?'没有路线匹配'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11303698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Routing Error? 'No route matches'
提问by user1497531
So I keep running into the following error:
所以我不断遇到以下错误:
No route matches {:action=>"show", :controller=>"users"}
No route matches {:action=>"show", :controller=>"users"}
I tried running rake routes and I can see that the route exists:
我尝试运行 rake 路线,我可以看到该路线存在:
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
But every time I try visiting the page "/users/1"(1 being the user id), I get the above error. Any ideas? Thanks!
但是每次我尝试访问该页面"/users/1"(1 是用户 ID)时,都会出现上述错误。有任何想法吗?谢谢!
Here's my routes.rb:
这是我的routes.rb:
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
resources :users
resource :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
Here's my users_controller.rb:
这是我的users_controller.rb:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Paper Piazza!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def index
@users = User.paginate(page: params[:page])
end
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
回答by fearless_fool
Try fixing the typo:
尝试修复错字:
root :to 'static_pages#home'
(rather than root to:), and moving it to the last line of the block. And let me know if that makes a difference!
(而不是root to:),并将其移动到块的最后一行。如果这有什么不同,请告诉我!
What's odd is that I built a fresh project with a routing file that simply reads:
奇怪的是,我用一个简单的路由文件构建了一个新项目:
RoutingTest::Application.routes.draw do
resources :users
root :to => "static_pages#home"
end
When I ran this in the console, I got the same error you're seeing:
当我在控制台中运行它时,我遇到了与您看到的相同的错误:
>> r = Rails.application.routes ; true
=> true
>> r.recognize_path("/users/1")
=> ActionController::RoutingError: No route matches "/users"
... but when I run the same thing in a slightly older project, I get:
...但是当我在一个稍旧的项目中运行同样的事情时,我得到:
>> r = Rails.application.routes ; true
=> true
>> r.recognize_path("/users/1")
=> {:action=>"show", :controller=>"users", :id=>"1"}
So I have little confidence that what I'm telling you will make a difference. (Aside from that, the Rails.application.routes trick is useful for verifying paths in the console!)
所以我不太相信我告诉你的会有所作为。(除此之外,Rails.application.routes 技巧对于在控制台中验证路径很有用!)
回答by raoul_dev
I had a similar problem, I had routes that would just return the root page, but would show up in rake routes. According to a fellow developer for a specific namespace and for all routes the root to: (routing)must always be last. This is in contradiction to the Rails guide on routing (see http://guides.rubyonrails.org/routing.html#using-root) which says that any call to root should be at the top of your file. That was definitely not working for me. Interesting...
我有一个类似的问题,我的路由只会返回根页面,但会显示在rake routes. 根据特定命名空间和所有路由的开发人员同事的说法,root to: (routing)必须始终放在最后。这与 Rails 路由指南(参见http://guides.rubyonrails.org/routing.html#using-root)相矛盾,该指南指出对 root 的任何调用都应位于文件的顶部。那绝对不适合我。有趣的...

