Topic: SOLVED - Rails 3.2.11, Ransack and namespaced routes… Template missing

Fairly new to Rails so sorry if the question seems obvious...

I have a User model in app/models

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :remember_me, :created_by, :active, :blocked, :profile_attributes
  has_secure_password


  # relations
  has_one :profile, :dependent => :destroy, :inverse_of => :user
  accepts_nested_attributes_for :profile

 [...]

And a controller in controllers/backstage/users_controller.rb

class Backstage::UsersController < ApplicationController

  def index
    #@users = User.all
    @q = User.search(params[:q])
    @users = @q.result(:distinct => true).paginate(:per_page => 5, :page => params[:page])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

[...]

# Search
  def search
    index
    render :index
  end
end

And the routes.rb file is

Novaxones::Application.routes.draw do

  resources :sessions, only: [:new, :create, :destroy]

  resources :users, :controller => "frontend/users", only: [:new, :create] do
    member do
      get "activate"
    end
  end


  resources :forgotten_passwords, :controller => "frontend/forgotten_passwords", except: [:destroy, :show]
  resources :activation_requests, :controller => "frontend/activation_requests", only: [:new, :create]

  match '/forgotten_password_request', to: 'frontend/forgotten_passwords#new'
  match '/logout', to: 'sessions#destroy', via: :delete
  match '/login', to: 'sessions#new'
  match '/registration', to: 'frontend/users#new'
  match '/contact', to: 'frontend/base_pages#contact'
  match '/about', to: 'frontend/base_pages#about'
  match '/help', to: 'frontend/base_pages#help'
  match '/site-map', to: 'frontend/base_pages#sitemap', as: 'sitemap'

  namespace :backstage do
    match '', to: "dashboard#index", as: '/'
    get 'users/active' => 'users#active', :as => 'active_users'
    resources :users do
      collection do
        match 'search' => 'users#search', :via => [:get, :post], :as => :search
      end
      resources :profiles
    end
  end


  root to: 'frontend/base_pages#home'

As you can see I use namespaced routes to have an admin panel (called backstage)

I use Ransack 0.7.2.  When I search anything I always have the error :

Missing template backstage/users/search, application/search with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/RailsApps/novaxones/app/views"

The index.html.erb is

<div id="table-users-grid">
  <div id="grid-top-pagination" class="row">

    <div class="span4"><%= will_paginate %></div>
    <div class="span4 page-entries-info"><%= page_entries_info %></div>
    <div id="search-area" class="span4">
      <%= search_form_for @q, :url => search_backstage_users_path, :html => {:method => :post} do |sf| %>
          <div class="search-form-fields">
            <%= sf.text_field :email_cont, :placeholder => 'search users email contains' %>
          </div>
          <div class="actions"><%= sf.submit "Search" %></div>
      <% end %>
    </div>
  </div>
  <div id="management-area" class="row">
    <table id="users-grid" class="span10">
      <thead>
      <tr>
        <th><%= link_to 'UID', :sort => "id" %></th>
        <th><%= link_to 'Last name', :sort => "profiles.last_name" %></th>
        <th><%= link_to 'First name', :sort => "profiles.first_name" %></th>
        <th><%= link_to 'Email', :sort => "email" %></th>
        <th><%= link_to 'Country', :sort => "profiles.country" %></th>
        <th>active</th>
        <th>blocked</th>
        <th>created_at</th>
        <th>created_by</th>
      </tr>
      </thead>
      <tbody>
      <% @users.each do |user| %>
          <tr>
            <td><%= user.id %></td>
            <td><%= user.profile.last_name %></td>
            <td><%= user.profile.first_name %></td>
            <td><%= user.email %></td>
            <td><%= user.profile.country %></td>
            <td><%= user.active %></td>
            <td><%= user.blocked %></td>
            <td><%= user.created_at.strftime("%d %B %Y") %></td>
            <td><%= user.created_by %></td>
            <td><%= link_to 'Show', backstage_user_profile_path(user, user.profile) %></td>
            <td><%= link_to 'Edit', edit_backstage_user_profile_path(user, user.profile) %></td>
            <td><%= link_to 'Destroy', backstage_user_path(user), confirm: 'Are you sure?', method: :delete %></td>
          </tr>
      <% end %>
      </tbody>
      <tfoot></tfoot>
    </table>
    <aside id="switch-view-wrapper" class="btn-group btn-group-vertical span2" data-toggle="buttons-radio">
      <%= link_to 'All', backstage_users_path, :class => 'btn' %>
      <%= link_to 'Active', backstage_active_users_path, :class => 'btn' %>
      <%= link_to 'Inactive', backstage_active_users_path, :class => 'btn' %>
      <%= link_to 'Blocked', backstage_active_users_path, :class => 'btn' %>
      <%= link_to 'Not Blocked', backstage_active_users_path, :class => 'btn' %>
    </aside>
  </div>

  <div id="grid-bottom-pagination">
    <%= will_paginate %>
  </div>
</div>

I probably missing or misunderstood something but cannot find a way to solve that...
I need some help and explanations here.
Thanks in advance

Cheers

Last edited by anso (2013-03-05 07:50:39)

Re: SOLVED - Rails 3.2.11, Ransack and namespaced routes… Template missing

The error is "Missing template backstage/users/search" which means it can not find a search.html.erb view template in your views/backstage/users folder
Create one, fill it with whatever it need filling with and your error will go. Hopefully not to be replaced by a new error smile

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: SOLVED - Rails 3.2.11, Ransack and namespaced routes… Template missing

Hi James
Thanks for answering. I know the problem comes from the routes and Rails is looking for a 'search' in mines.
My problem is with the 'virtual' routes .
What I don't understand is why this one

resources :users, :controller => "frontend/users", only: [:new, :create] do
    member do
      get "activate"
    end
  end

works fine even if there is no 'activate.html.erb'

and in this one

namespace :backstage do
    match '', to: "dashboard#index", as: '/'
    get 'users/active' => 'users#active', :as => 'active_users'
    resources :users do
      collection do
        match 'search' => 'users#search', :via => [:get, :post], :as => :search
      end
      resources :profiles
    end
  end

Why the part

    resources :users do
      collection do
        match 'search' => 'users#search', :via => [:get, :post], :as => :search
      end

does not work... I supposed that the block "collection do...end" act as the block "member do...end"  but it seems that is not the case... Probably my fault but I don't understand why....

Cheers

Re: SOLVED - Rails 3.2.11, Ransack and namespaced routes… Template missing

I solved it !
The problem was in my controller action...
I changed it to

@q = User.search(params[:q])
@users = @q.result(:distinct => true).paginate(:per_page => 5, :page => params[:page])
render 'index'

and now all is fine. Thanks for your time