【问题标题】:Unable to find working route - No route matches {:action=>"show", :controller=>"users"}无法找到工作路线 - 没有路线匹配 {:action=>\"show\", :controller=>\"users\"}
【发布时间】:2022-12-07 07:33:14
【问题描述】:

没有路由匹配 {:action=>"show", :controller=>"users"}

使用这种格式在 rails 中使用条件 current_page。尝试不在 users/show 路径中呈现导航栏,但它应该在站点的其余部分可见。需要注意的一件事是 users/show URL 已在 routes.rb 中配置为不在 URL 中显示“/users/”文件夹,因此它看起来像“mysite.com/username”

    <% if current_page?(controller: 'users', action: 'show') %>
    no navbar
    <% else %>
    <%= render partial: "shared/navbar" %>
    <% end %>

第一个条件工作正常,但是当我到达一个应该匹配“其他”条件的页面时,例如我的 root_path,我得到这个错误:

    ActionController::UrlGenerationError in Dashboard#show
    Showing /Users/javier/Desktop/rails-apps/testtradus3/app/views/shared/_navbar.html.erb where line #1 raised:

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

  

我的 route.rb 看起来像这样

    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    Rails.application.routes.draw do
    ...

      # This removes '/users/' from user/show URI
      resources :users, path: '', :only => [:show]

      # User account
      devise_for :users,
        controllers: {
          omniauth_callbacks: "users/omniauth_callbacks",
          registrations: "users/registrations",
          sessions: "users/sessions"
        }
      devise_scope :user do
        get "session/otp", to: "sessions#otp"
      end

      resources :users do
        resources :builduser, controller: 'users/builduser'
      end

    ...

    end

这将返回此轨道路线:

用户 GET /users(.:format) users#index POST /users(.:format) 用户#create

我试过删除 routes.rb 中的自定义路径,所以像 resources :users 这样的东西会返回这些路由 用户 GET /users(.:format) users#index POST /users(.:format) 用户#create

          GET    /users(.:format)                                                                                  users#index
          POST   /users(.:format)                                                                                 users#create
          GET    /users/new(.:format)                                                                              users#new
          GET    /users/:id/edit(.:format)                                                                         users#edit
          GET    /users/:id(.:format)                                                                              users#show

我的 UsersController.rb

    class UsersController < ApplicationController

        def index
            @users = User.all
        end

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


        def create
            @user = User.new(user_params)
            
            respond_to do |format|
                if @user.save
                # format.html { redirect_to @order, notice: "Order was successfully created." }
                # Added this one below:
                format.html { redirect_to user_builduser_index_path(@user)}
                format.json { render :show, status: :created, location: @user }
                else
                format.html { render :new, status: :unprocessable_entity }
                format.json { render json: @user.errors, status: :unprocessable_entity }
                end      
            end
        end
    ..
    end

【问题讨论】:

  • 如果您让用户在您的站点上链接到 /user_name_here,要记住的一件事是,如果它变得流行,您将排挤自己的声明路线选项。虽然您可以使用广泛的允许名称黑名单,但很难真正知道您将来需要什么。例如,Reddit 很聪明地使用了/u/username

标签: ruby-on-rails


【解决方案1】:

关闭下面链接中列出的一些示例,您可以尝试:

current_page?(users_path(current_user))
# or
current_page?(users_path(@user))
# or even
current_page?("/#{@user&.id}")

另一种方法是在 ApplicationController 中设置 before_action 以设置实例变量:

before_action :display_nav_bar # this would apply to all routes unless overridden like below in the UsersController

def display_nav_bar
  @display_nav_bar = true
end

# and in your UsersController:

def show
  @display_nav_bar = false
  # other stuff
  @user = User.find(params[:id)
end

然后

<% if @display_nav_bar %>
    <%= render partial: "shared/navbar" %>
<% else %>
    no navbar
<% end %>

您还可以考虑为不同的控制器使用布局并控制以这种方式呈现导航栏。

current_page? source

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2018-09-22
    相关资源
    最近更新 更多