轨道 4 用户路由


<%= link_to "Whatever", current_user %>

正在链接到/user.id

我的路线是这样设置的

resource :user, except: [:index, :destroy]

所以它应该链接到/user,对吧?

当我访问/user时,它说"找不到没有ID的用户"。

我的用户显示操作如下所示

def show
    @user = User.find(params[:id])
end

您获得/user.id的原因是因为您已将路由定义为

resource :users, except: [:index, :destroy]

请注意singular resource,它将创建没有任何:id的所有路由。由于路由中没有接受参数,因此您传递的current_userformat匹配,即,就像.html, .js, etc.一样,在您的情况下会变成.id

我建议使用resources(注意复数)

resources :users, except: [:index, :destroy]

这将解决错误Couldn't find User without an ID,因为您将在路由中传递参数id

注意:

根据 Rails 约定,控制器名称应plural 。 对于UsersController,资源应该resources :users

这种情况发生过很多次。我的解决方法是使用路径助手。

<% link_to "Whatever", user_path current_user %>

这将删除.id并使其/user/id

最新更新