使用跨模型范围在索引页面上过滤表单
我想使用一个表单来过滤索引页面,该表单使用复选框进行过滤而不刷新页面。 这是Users
索引,但我根据Profile
数据进行过滤。 我是编程的初学者,并且已经设法将我正在尝试做的一些事情拼凑起来,但需要帮助将它们连接起来。 (供参考,有关我使用本文的范围的指导以及我使用本文的表单/ ajax / jQuery。)
User.rb:
class User :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 [conditions, params], :offset => params[:offset], :limit => params[:limit] ) respond_with @users end end
ProfilesController:
class ProfilesController [: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
index.html.erb中的示例:
'users_index', :method => :get, :remote => true do %> 'submittable' %>
我的用户在index.html.erb上呈现的位置:
index.js.erb的:
$("#wall").html("");
有人可以帮我弄清楚我做错了什么吗?
-
form_for @user
调用update。 你应该这样做:= form_tag“/ users / search”,:method =>:get
-
??? 我想我们需要看看你的javascript代码。
总的来说,我强烈建议你把你的代码放在一边,并按照ryan bate的截屏video进行高级搜索 ,有很多东西需要解决/改进,就像现在一样。