没有路由匹配{:action =>“schedule”,:controller =>“users”}

时间:2013-05-22 08:10:57

标签: ruby-on-rails-3 routes

我有一个奇怪的问题:我在登录后尝试返回主页localhost:3000/

后收到以下错误

“没有路线匹配{:action =>”schedule“,:controller =>”users“}”

这是routes.rb:

Ikky::Application.routes.draw do
  root to: 'static_pages#home'

  resources :users do
    member do
      get 'schedule'
    end
  end

rake routes返回以下内容:

         root        /                             static_pages#home
schedule_user GET    /users/:id/schedule(.:format) users#schedule
        users GET    /users(.:format)              users#index
              POST   /users(.:format)              users#create
[...]

引入我的schedule_user路线时出现问题。

这是我的users_controller:

class UsersController < ApplicationController

before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :schedule]
before_filter :correct_user,   only: [:edit, :update]
before_filter :admin_user,     only: :destroy

  def schedule
    @user = User.find(params[:id])
    @spots = @user.spots.paginate(page: params[:page])
    @spot = current_user.spots.build if signed_in?
  end

这是static_pages_controller:

class StaticPagesController < ApplicationController

  def home
    @spot = current_user.spots.build if signed_in?
  end

这是部分共享/ user_info:

<a href="<%= user_path(current_user) %>">

  <%= gravatar_for current_user, size: 52 %>
</a>
<h1>
  <%= current_user.name %>
</h1>
<span>
  <%= link_to "view my profile", current_user %>
</span>
<span>
  <%= pluralize(current_user.spots.count, "spots") %>
</span>

感谢您的帮助!

P.S:我是初学者,如果我的代码很乱,我很抱歉...

1 个答案:

答案 0 :(得分:0)

通常情况下,如果我们将member路径放在id之内,就会发生这种情况。例如:link_to "A", 'users/schedule' or schedule_user_path

通过更正指向'users/#{user.id}/schedule'schedule_user_path(user.id)

的链接解决此问题