【问题标题】:Rails Routing Error? 'No route matches'Rails 路由错误? '没有路线匹配'
【发布时间】:2012-07-03 02:27:24
【问题描述】:

所以我一直遇到以下错误:

No route matches {: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)时,都会出现上述错误。有任何想法吗?谢谢!

这是我的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'

这是我的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

【问题讨论】:

  • 显示你的 routes.rb 文件
  • 不,/users/1/ 似乎不起作用..
  • 你有users_controller.rb吗?
  • 是的,我也将其发布。
  • 这是您得到的确切错误吗?这个问题可能相关吗? stackoverflow.com/questions/10217093/…

标签: ruby-on-rails rails-routing


【解决方案1】:

尝试修正错字:

root :to 'static_pages#home'

(而不是root to:),并将其移动到块的最后一行。如果这有什么不同,请告诉我!

奇怪的是,我用一个简单的路由文件构建了一个新项目:

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 技巧对于在控制台中验证路径很有用!)

【讨论】:

  • 小心保留名称。尝试不使用 static_pages 看看你会得到什么。
  • root to: 'static_pages#home' 是正确的语法,
【解决方案2】:

我遇到了类似的问题,我的路由只会返回根页面,但会显示在 rake routes 中。根据特定命名空间和所有路由的其他开发人员的说法,root to: (routing) 必须始终位于最后。这与 Rails 路由指南相矛盾(请参阅http://guides.rubyonrails.org/routing.html#using-root),该指南说对 root 的任何调用都应位于文件的顶部。那绝对不适合我。有趣...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多