No route matches {:action=>"show", :controller=>"users", :id=>nil, :locale=>:ru}, possible unmatched constraints: [:id]

huangapple go评论63阅读模式
英文:

No route matches {:action=>"show", :controller=>"users", :id=>nil, :locale=>:ru}, possible unmatched constraints: [:id]

问题

Here's the translated portion of your text:

我添加了通过应用程序中的按钮(链接)登录帐户的功能,但是使用正确的路由时,我收到了一个错误:

> ActionController::UrlGenerationError in Posts#index
> Showing /home/artem/projects/blog/app/views/layouts/application.html.erb where line #44 raised:
> No route matches {:action=>"show", :controller=>"users", :id=>nil, :locale=>:ru}, possible unmatched constraints: [: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/}

如果您需要额外的信息,请在评论中提出。

英文:

I added the ability to log into the account through the button (link) in the application, but with the correct routes, I get an error:
> ActionController::UrlGenerationError in Posts#index
> Showing /home/artem/projects/blog/app/views/layouts/application.html.erb where line #44 raised:
> No route matches {:action=>"show", :controller=>"users", :id=>nil, :locale=>:ru}, possible unmatched constraints: [:id]

But if I enter the link manually (in the search field of browser) - it works (provided that the id is correct)

My 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

My link(in application.html.erb):
<%= link_to t('.Profile'), user_path(@user), class: "btn btn-outline-#{cookies[:theme] == "dark" ? "light": "dark"} mx-2" %>

My routes:

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/}

If you need additional information - write in the comments

I tried: users_path(@users) for link to index.html.erb. That worked correct

答案1

得分: 0

Your variable @user is at the most of your pages not initialized therefore it returns nil (undefined instance variables do not raise an exception) and therefore your path to the user profile is not correct (does not contain user_id).

If you want to add a link to the user's profile page to all your pages, you have to have the user object or at least user_id available from all controller methods rendering application.html.erb layout.

I see you have implemented your authentication with Devise. Then you can use its current_user method in your views to get the user object for the currently signed-in user. Your link would look like this: link_to t('.Profile'), user_path(current_user), ...

英文:

Your variable @user is at the most of your pages not initialiazed therefore it returns nil (undefined instance variables do not raise an exception) and therefore you path to the user profile is not correct (does not contain user_id).

If you want to add a link to the user's profile page to all your pages, you have to have the user object or at least user_id available from all controller methods rendering application.html.erb layout.

I see you have implemented your authentication with Devise. Then you can use it's current_user method in your views to get the user object for the currently signed in user. Your link would look like this: link_to t('.Profile'), user_path(current_user), ...

huangapple
  • 本文由 发表于 2023年6月9日 00:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定