Form filter on index page using cross-model scope

2019-08-05 05:06发布

问题:

I want to filter an index page using a form that filters using checkboxes without refreshing the page. It's the Users index but I'm filtering depending on Profile data. I'm a beginner to programming and have managed to piece together some of what I'm trying to do but need help connecting it all. (For reference, for guidance on scopes I used this article and for the form/ajax/jQuery I used this article.)

User.rb:

class User < ActiveRecord::Base
  has_one :profile, :dependent => :destroy
  scope :profiles, lambda {
    joins(:profiles).group("users.id") & Profile.id
  }
end

Profile.rb:

class Profile < ActiveRecord::Base
  belongs_to :user
  class << self
    def search(q)
      [:high_school, :higher_ed, :current_city, :job_title, :hometown, :subject].inject(scoped) do |combined_scope, attr|
        combined_scope.where("profiles.#{attr} LIKE ?", "%#{q}%")
      end
    end
  end
end

UsersController:

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def update
    @user = current_user
    @user.update_attributes(params[:user])
  end

  def search
    if params[:profiles].blank?
      raise "You must provide search criteria."
    end
    params[:profiles] = "%#{params[:profiles]}%"
    conditions    = " Description LIKE :term"

    @users = User.all(
        :conditions => [conditions, params],
        :offset     => params[:offset],
        :limit      => params[:limit]
    )

    respond_with @users
  end
end

ProfilesController:

class ProfilesController < ApplicationController
  before_filter :authenticate, :only => [:edit, :update]

  def index
    @profile = current_user.profile
  end

  def update
    @profile = user.profile
    if @profile.update_attributes(params[:profile])
      redirect_to profile_path, :notice => 'Updated user information successfully.'
    else
      render :action => 'edit'
    end
  end
end

Routes.rb:

resources :users do
    get "search/:term/:offset/:limit.:format", :action => "search", :constraints => { :offset => /\d+/, :limit => /\d+/ }
end

Sample from index.html.erb:

<%= form_tag users_path, :id => 'users_index', :method => :get, :remote => true  do %>
<table>
  <tr>
    <td class="normal"><%= current_user.profile.high_school %></td>
    <td><%= check_box_tag 'high_school', :class => 'submittable' %></td>
  </tr>
</table>
<% end %>

Where my Users render on index.html.erb:

<div class="searchresults">
  <div id="wall">
    <% @users.each do |user| %>
    <% end %>
  </div>
</div>

index.js.erb:

$("#wall").html("<%= escape_javascript(render(@users)) %>");

Can someone help me figure out what I'm doing wrong?

回答1:

  1. form_for @user calls update. You should do this instead :

    =form_tag "/users/search", :method => :get

  2. ??? I think we would need to see your javascript code.

Overall I strongly suggest you put your code aside and follow ryan bate's screencast for advanced search form, there's to many things to be solved/improved as it is now.