malditojavi
malditojavi

Reputation: 1114

Unable to find working route - No route matches {:action=>"show", :controller=>"users"}

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

Working with a condition current_page in rails with this format. Trying to not render a navbar within users/show path, but it should be visible in the rest of site. One thing to note is that the users/show URL has been configured in routes.rb to not show '/users/' folder in the URL, so it looks like 'mysite.com/username'

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

The first condition works fine, however when I reach a page that should match the 'else' condition, for instance my root_path, I get this error:

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

  

My route.rb looks like this

    # 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

This returns this rails routes:

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

I have tried removing the custom path in routes.rb, so something like resources :users and that returns these routes 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

My 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

Upvotes: 1

Views: 254

Answers (1)

abax
abax

Reputation: 787

Going off some of the examples listed in the link below, you could try:

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

An alternative could be to set a before_action in your ApplicationController to set an instance variable:

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

then

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

You could also look into using layouts for different controllers and control rendering the nav bar that way.

current_page? source

Upvotes: 0

Related Questions