问题
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"
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)
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
EDIT AND SOLVED
The problem was in the search action of my controller
Change from
# Search
def search
index
render :index
end
end
to
# Search
def search
@q = User.search(params[:q])
@users = @q.result(:distinct => true).paginate(:per_page => 5, :page => params[:page])
render 'index'
end
And all is fine now.
I had to add it to the scopes too, for those who are interested for the scope active in the controller the code become
def active
@q = User.search(params[:q])
@users = @q.result(:distinct => true).active.paginate(:per_page => 5, :page => params[:page])
render 'index'
end
and is defined as below in the user model
# scopes
scope :active, joins(:profile).where(:active => true)
Hope this can be helpful for someone else
Cheers
来源:https://stackoverflow.com/questions/15206804/ransack-and-namespaced-routes-template-missing