非常坚持'无路线匹配错误'

时间:2012-04-09 23:53:36

标签: ruby-on-rails

我是Ruby on Rails的新手并且不断收到以下错误消息:

ActionController::RoutingError in Users#index 

"No route matches {:action=>"following", :controller=>"users"}"

Showing /app/views/layouts/_navbar.html.erb......where line #19 raised:

Extracted source (around line #19):
16: </td>
17:                 </div>
18:        
19:                 <div id="secondlink"><td class="rite"><li><% if signed_in? %><%= link_to "Profile", current_user %></li><li><%= link_to "watching", following_user_path(@user)%></li><li><%= link_to "Users", users_path %></li><li><%= link_to "Sign out", signout_path, :method => :delete %>
20:       <% else %>
21: <%= link_to "Sign in", signin_path %></li><% end %>
22:      

当我点击链接(在我的导航栏中)以显示分页用户的完整列表时,会发生此路由错误“

我的routes.rb文件中包含以下内容:

 Mvp::Application.routes.draw do
   resources :users do
    member do
      get :following, :followers
    end
   end
在UsersController中执行

“关注”操作:

def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.following.paginate(:page => params[:page])
    render 'show_follow'
end

2 个答案:

答案 0 :(得分:2)

我不能肯定地说,但我的第一个猜测是在下面的

<%= link_to "watching", following_user_path(@user)%>

变量@user为零。我不知道为什么它是零,但如果它是零则这是你会得到的错误。

答案 1 :(得分:0)

rake routes输出

following_user GET    /users/:id/following(.:format) users#following
followers_user GET    /users/:id/followers(.:format) users#followers
         users GET    /users(.:format)               users#index
               POST   /users(.:format)               users#create
      new_user GET    /users/new(.:format)           users#new
     edit_user GET    /users/:id/edit(.:format)      users#edit
          user GET    /users/:id(.:format)           users#show
               PUT    /users/:id(.:format)           users#update
               DELETE /users/:id(.:format)           users#destroy

所以你得到了你的路线 - 定义了控制器映射。

接下来要检查的事项:

  • 您的following已定义

  • 中有UsersController个动作吗?
  • 您是否使用GET方法

  • 请求服务器响应