没有匹配的路由{:action=〉“显示”,:controller=〉“用户”}
在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 %>
第一个条件工作正常,但是当我到达一个应该匹配'else'条件的页面时,例如我的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"}
我的路线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 /用户(.:格式)用户#索引POST /用户(.:格式)用户#创建
我试过删除routes.rb中的自定义路径,比如resources:users,它返回这些路径users GET /users(.:format)users#index POST /users(.:format)users#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
我的用户控制器.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
1条答案
按热度按时间lb3vh1jj1#
下面的链接中列出了一些例子,你可以尝试:
另一种方法是在
ApplicationController
中设置before_action
以设置示例变量:那么
你也可以考虑为不同的控制器使用布局,并以这种方式控制导航栏的渲染。
current_page? source