在索引页上使用交叉模型范围进行表单过滤 [英] Form filter on index page using cross-model scope

查看:187
本文介绍了在索引页上使用交叉模型范围进行表单过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤一个索引页面,使用复选框进行筛选而不刷新页面。这是 Users 索引,但是我依据 Profile 数据过滤。我是一个初学者编程,并设法把我想要做的一些东西拼凑在一起,但需要帮助连接这一切。 (作为参考,我使用的范围指南这篇文章和我使用的表单/ ajax / jQuery 这篇文章。)

User.rb:

  class User< ActiveRecord :: Base 
has_one:profile,:dependent => :销毁
范围:profiles,lambda {
连接(: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
$ b $ def update
@user = current_user
@ user.update_attributes(params [:user])
结束

def搜索
if params [:profiles] .blank?
提高您必须提供搜索条件。
end
params [:profiles] =%#{params [:profiles]}%
conditions =描述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
$ b def update
@profile = user .profile
if @ profile.update_attributes(params [:profile])
redirect_to profile_path,:notice => '成功更新用户信息'。
else
render:action => 'edit'
end
end
end

Routes.rb:

  resources:users do 
getsearch /:term /:offset / :limit。:format,:action => 搜索,:约束=> {:offset => / \d + /,:limit => / \d + /}
结束

来自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%>

在用户在index.html.erb上呈现的位置:

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

index.js.erb: $ b

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

有人可以帮我弄清楚我做错了什么?

解决方案


  1. form_for @user 您应该这样做:
    $ b $ p $ form_tag/ users / search,:method =>:get


  2. ???我认为我们需要看到你的javascript代码。
    总共我强烈建议你把你的代码放在一边, ryan bate的高级搜索表单的屏幕截图,有很多事情需要解决/改进现在是。


    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. 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.

    这篇关于在索引页上使用交叉模型范围进行表单过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆