当我搜索任何内容时,我总是有错误:
缺少模板后台/用户/搜索,应用程序/搜索 {:locale => [:en] ,: formats => [:html],:handlers => [:erb,:builder, :咖啡]}。搜索:*" 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)
希望这对其他人有帮助
干杯