使用ransack搜索用户个人资料

时间:2016-03-30 12:20:04

标签: ruby-on-rails ransack

我有两个模型,userprofile。用户有一个个人资料。

# profile.rb
class Profile < ActiveRecord::Base
  belongs_to :user
end

# user.rb
class User < ActiveRecord::Base
  has_one :profile
end

# routes.rb
resources :users do
  resource :profiles, except: [:index, :show]
end

# users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.includes(:profile)
  end
end

# users/index.html.erb
<% @users.each do |user| %>
  <% if user.profile %>
    <%= user.name %>
    <%= user.interest %>
  <% end %>
<% end %>

现在,我想添加ransack gem来搜索用户个人资料。这是我目前的设置:

# routes.rb
resources :users do
  collection do
      match 'search' => 'users#search', via: [:get, :post], as: :search
  end
  resource :profile, except: [:index, :show]
end

# users_controller.rb
class UsersController < ApplicationController
  def index
    @search = User.ransack(params[:q])
    @users = @search.result.includes(:profile)
  end

  def search
    index
    render :index
  end
end

# users/index.html.erb
<%= search_form_for @search, url: search_users_path, method: :post, do |f| %>
  <%= f.search_field :name_cont, placeholder: 'Name' %><br>
  <%= f.search_field :interest_cont, placeholder: 'Hobby' %><br>
  <%= f.submit 'Search %>
<% end %>

但是我收到了这个错误:

NoMethodError in Users#index

undefined method `name_cont' for Ransack::Search<class: User, base: Grouping <combinator: and>>:Ransack::Search

<%= f.search_field :name_cont, placeholder: 'Name' %><br>

我的代码出了什么问题?我应该将搜索路径嵌套到配置文件而不是用户,所以它看起来像这样:

# routes.rb
resources :users do
  resource :profile, except: [:index, :show] do
    match 'search' => 'profiles#search', via: [:get, :post], as: :search
  end
end

然后,如何设置其余的?谢谢。

2 个答案:

答案 0 :(得分:0)

您需要在用户模型中添加ransacker方法。可以找到示例here

在User.rb

ransacker :name_cont, formatter: proc { |v|
  data = User.joins(:profile).where('profile.name = ?', v).map(&:id)
  data = data.present? ? data : nil
}, splat_param: true do |parent|
 parent.table[:id]
end

我还没有测试过这段代码。

答案 1 :(得分:0)

我的错误,我没有仔细阅读文档。我只需要在视图中使用这些:

<%= search_form_for @search, url: search_users_path, method: :post, do |f| %>
  <%= f.search_field :profile_name_cont, placeholder: 'Name' %><br>
  <%= f.search_field :profile_interest_cont, placeholder: 'Hobby' %><br>
  <%= f.submit 'Search %>
<% end %>