Ruby on rails 轨道路由错误';没有路由匹配';
因此,我一直遇到以下错误:Ruby on rails 轨道路由错误';没有路由匹配';,ruby-on-rails,rails-routing,Ruby On Rails,Rails Routing,因此,我一直遇到以下错误: 没有路由匹配{:action=>“show”,:controller=>“users”} 我尝试运行rake路由,我可以看到该路由存在: user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:
没有路由匹配{:action=>“show”,:controller=>“users”}
我尝试运行rake路由,我可以看到该路由存在:
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
但是每次我尝试访问页面“/users/1”
(1是用户id),我都会遇到上述错误。有什么想法吗?谢谢
这是我的路线。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'
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
这是我的用户\u控制器.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'
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
class UsersController
尝试修复打字错误:
root :to 'static_pages#home'
(而不是根目录:
),并将其移动到块的最后一行。如果有什么不同,请告诉我
奇怪的是,我构建了一个新项目,其中包含一个路由文件,该文件的内容如下:
RoutingTest::Application.routes.draw do
resources :users
root :to => "static_pages#home"
end
当我在控制台中运行此命令时,出现了与您看到的相同的错误:
>> r = Rails.application.routes ; true
=> true
>> r.recognize_path("/users/1")
=> ActionController::RoutingError: No route matches "/users"
。。。但当我在一个稍旧的项目中运行相同的东西时,我得到:
>> r = Rails.application.routes ; true
=> true
>> r.recognize_path("/users/1")
=> {:action=>"show", :controller=>"users", :id=>"1"}
所以我没有信心我告诉你的事情会有所不同。(除此之外,Rails.application.routes技巧对于验证控制台中的路径非常有用!)我有一个类似的问题,我有一些只返回根页面的路由,但会显示在
rake routes
中。根据一位特定名称空间和所有路由的开发人员的说法,根到:(路由)
必须始终是最后一个。这与Rails路由指南(请参阅)相矛盾,该指南规定,对root的任何调用都应该位于文件的顶部。这对我来说绝对不管用。有趣的…显示您的路线。rb文件不,/users/1/
似乎不起作用。您是否有用户控制器。rb
?是的,我也会发布。这就是您遇到的错误吗?这个问题有关系吗?小心保留的名字。试着不要使用静态页面,看看你得到了什么。root to:'static_pages#home'
是正确的语法,