当我搜索任何东西时,我总是遇到错误:
缺少模板后台/用户/搜索,使用 {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]} 的应用程序/搜索。在以下位置搜索:*“C:/RailsApps/novaxones/app/views”
我在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
[...]
还有一个控制器在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
而且routes.rb文件是
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'
如您所见,我使用命名空间路由来拥有一个管理面板(称为后台)
index.html.erb是_
<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>
我可能遗漏或误解了某些东西,但找不到解决方法……我需要一些帮助和解释。预先感谢 干杯
编辑并解决
问题出在我的控制器的搜索操作中
从改变
# Search
def search
index
render :index
end
end
至
# Search
def search
@q = User.search(params[:q])
@users = @q.result(:distinct => true).paginate(:per_page => 5, :page => params[:page])
render 'index'
end
现在一切都很好。
我也必须将它添加到范围中,对于那些对控制器中活动范围感兴趣的人,代码变为
def active
@q = User.search(params[:q])
@users = @q.result(:distinct => true).active.paginate(:per_page => 5, :page => params[:page])
render 'index'
end
并在用户模型中定义如下
# scopes
scope :active, joins(:profile).where(:active => true)
希望这对其他人有帮助
干杯