我是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
答案 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
方法