我添加了通过应用程序中的按钮(链接)登录帐户的功能,但使用正确的路由,我得到一个错误:
/home/阿尔特姆/projects/blog/app/views/layouts/application.html.erb where line #44 raised:没有路由匹配{:action=>“show”,:controller=>“users”,:id=>nil,:locale=>:ru},可能不匹配的约束:[:id]
但如果我手动输入链接(在浏览器的搜索字段中)-它工作(前提是id正确)
我的users_controller.rb:
class UsersController < ApplicationController
def index
@user = User.all
end
def show
@user = User.find(params[:id])
@user_posts = @user.posts
end
end
我的链接(在application.html.erb中):<%= link_to t('.Profile'), user_path(@user), class: "btn btn-outline-#{cookies[:theme] == "dark" ? "light": "dark"} mx-2" %>
我的路线:
Rails.application.routes.draw do
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
root "posts#index"
devise_for :users
get 'set_theme', to: 'theme#update'
resources :users
resources :posts, only: [:show, :index]
resources :tags, only: [:show]
resources :categories, only: [:show]
namespace :admin do
resources :users
resources :categories, except: [:show]
resources :posts, except: [:show, :index]
end
end
end
rails routes |grep users
new_user_session GET (/:locale)/users/sign_in(.:format)
devise/sessions#new {:locale=>/ru|en/}
user_session POST (/:locale)/users/sign_in(.:format)
devise/sessions#create {:locale=>/ru|en/}
destroy_user_session DELETE (/:locale)/users/sign_out(.:format)
devise/sessions#destroy {:locale=>/ru|en/}
new_user_password GET (/:locale)/users/password/new(.:format)
devise/passwords#new {:locale=>/ru|en/}
edit_user_password GET (/:locale)/users/password/edit(.:format)
devise/passwords#edit {:locale=>/ru|en/}
user_password PATCH (/:locale)/users/password(.:format)
devise/passwords#update {:locale=>/ru|en/}
PUT (/:locale)/users/password(.:format)
devise/passwords#update {:locale=>/ru|en/}
POST (/:locale)/users/password(.:format)
devise/passwords#create {:locale=>/ru|en/}
cancel_user_registration GET (/:locale)/users/cancel(.:format)
devise/registrations#cancel {:locale=>/ru|en/}
new_user_registration GET (/:locale)/users/sign_up(.:format)
devise/registrations#new {:locale=>/ru|en/}
edit_user_registration GET (/:locale)/users/edit(.:format)
devise/registrations#edit {:locale=>/ru|en/}
user_registration PATCH (/:locale)/users(.:format)
devise/registrations#update {:locale=>/ru|en/}
PUT (/:locale)/users(.:format)
devise/registrations#update {:locale=>/ru|en/}
DELETE (/:locale)/users(.:format)
devise/registrations#destroy {:locale=>/ru|en/}
POST (/:locale)/users(.:format)
devise/registrations#create {:locale=>/ru|en/}
new_user_confirmation GET (/:locale)/users/confirmation/new(.:format)
devise/confirmations#new {:locale=>/ru|en/}
user_confirmation GET (/:locale)/users/confirmation(.:format)
devise/confirmations#show {:locale=>/ru|en/}
POST (/:locale)/users/confirmation(.:format)
devise/confirmations#create {:locale=>/ru|en/}
users GET (/:locale)/users(.:format)
users#index {:locale=>/ru|en/}
POST (/:locale)/users(.:format)
users#create {:locale=>/ru|en/}
new_user GET (/:locale)/users/new(.:format)
users#new {:locale=>/ru|en/}
edit_user GET (/:locale)/users/:id/edit(.:format)
users#edit {:locale=>/ru|en/}
user GET (/:locale)/users/:id(.:format)
users#show {:locale=>/ru|en/}
PATCH (/:locale)/users/:id(.:format)
users#update {:locale=>/ru|en/}
PUT (/:locale)/users/:id(.:format)
users#update {:locale=>/ru|en/}
DELETE (/:locale)/users/:id(.:format)
users#destroy {:locale=>/ru|en/}
admin_users GET (/:locale)/admin/users(.:format)
admin/users#index {:locale=>/ru|en/}
POST (/:locale)/admin/users(.:format)
admin/users#create {:locale=>/ru|en/}
new_admin_user GET (/:locale)/admin/users/new(.:format)
admin/users#new {:locale=>/ru|en/}
edit_admin_user GET (/:locale)/admin/users/:id/edit(.:format)
admin/users#edit {:locale=>/ru|en/}
admin_user GET (/:locale)/admin/users/:id(.:format)
admin/users#show {:locale=>/ru|en/}
PATCH (/:locale)/admin/users/:id(.:format)
admin/users#update {:locale=>/ru|en/}
PUT (/:locale)/admin/users/:id(.:format)
admin/users#update {:locale=>/ru|en/}
DELETE (/:locale)/admin/users/:id(.:format)
admin/users#destroy {:locale=>/ru|en/}
如果您需要其他信息-请在备注中填写
我尝试了:users_path(@users)链接到index. html. erb。这是正确的
1条答案
按热度按时间ylamdve61#
您的变量
@user
在大多数页面中未初始化,因此它返回nil
(未定义的示例变量不会引发异常),因此您的用户配置文件路径不正确(不包含user_id
)。如果你想在你的所有页面中添加一个指向用户个人资料页面的链接,你必须让用户对象或者至少
user_id
可以从所有呈现application.html.erb
布局的控制器方法中获得。我看到您已使用Devise实施了身份验证。然后可以在视图中使用它的
current_user
方法来获取当前登录用户的user对象。您的链接看起来像这样:link_to t('.Profile'), user_path(current_user), ...