Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:基于参数的集合_Ruby On Rails_Ruby On Rails 3_Model View Controller_Routing - Fatal编程技术网

Ruby on rails Rails:基于参数的集合

Ruby on rails Rails:基于参数的集合,ruby-on-rails,ruby-on-rails-3,model-view-controller,routing,Ruby On Rails,Ruby On Rails 3,Model View Controller,Routing,我有一个索引方法,看起来非常像这样: def index if params[:brand] @users = User.includes(:brand).where(brand_id: params[:brand]).order("#{sort_column} #{sort_direction}").page(params[:page]).per(10) elsif params[:search] @user = User.includes(:bra

我有一个索引方法,看起来非常像这样:

  def index
    if params[:brand]
      @users = User.includes(:brand).where(brand_id: params[:brand]).order("#{sort_column} #{sort_direction}").page(params[:page]).per(10)
    elsif params[:search]
      @user = User.includes(:brand).find_by_client_code(params[:search])
      redirect_to edit_user_path(@user)
    elsif params[:page] == 'all'
      @users = User.includes(:brand).order("#{sort_column} #{sort_direction}").all
    elsif params[:state]
      @users = User.includes(:brand).where(state: params[:state]).page(params[:page]).per(10)
    else
      @users = User.includes(:brand).order("#{sort_column} #{sort_direction}").page(params[:page]).per(10)
    end
  end
def index
  if params[:search].present?
    @user = User.find_by_client_code(params[:search])
    redirect_to edit_user_path(@user)
  else
    @users = User.fetch_with_brand(user_params)
  end
end
我知道很乱,但它起作用了。现在我正在尝试重构它,我无法找到最好的方法将它拆分为更小的集合,而不使我的路线复杂化

  def index
    [:brand, :page, :search, :state].each do |param|
      if params[:page] == 'all'
        @users = User.includes(:brand).order(column + ' ' + direction)
      elsif params.key?(param)
        param
      else
        @users = User.includes(:brand).order(column + ' ' + direction)
                     .page(params[:page]).per(10)
      end
    end
  end

  def brand
    @users = User.includes(:brand).where('brand_id in (?)', params[:brand])
                 .order(column + ' ' + direction).page(params[:page]).per(10)
  end

  def state
    @users = User.includes(:brand).where(state: params[:state])
                 .page(params[:page]).per(10)
  end

  def search
    @user = User.includes(:brand).find_by_client_code(params[:search])
    redirect_to edit_user_path(@user)
  end

上述方法不起作用,但你明白了。有人知道处理这种情况的好方法吗?干杯。

我可能会这样做-

首先,在定义了
sort\u列
sort\u方向
方法的地方更新此代码,使其具有默认值:

def sort_column
  colum_name = params[:colum_name]
  colum_name ||= 'id'
end

def  sort_direction
  direction = params[:direction]
  direction ||= 'ASC'
end
添加一个新方法,使每个页面都有
(位于
sort\u列和
sort\u方向的同一位置)来自params或默认来自
User
类:

def per_page
  per = params[:per_page]
  per ||= User.per_page
end
在app/models/user.rb中:

您是否在上面提到的代码中使用以下行:
[self.scoped,(page!=“all”)]
?这里的
self.scoped
等于
self.all
(在计算时),但是我们必须使用
scoped
而不是
all
,因为在Rails 3中,它给出了一个数组,而在Rails 4中,它将是一个ActiveRecord::Relation对象,因此如果您在Rails 4上,您可以使用
self.all
。注意:
scoped
在Rails 4中被弃用,取而代之的是
all

还有,我想指出一个问题。在代码中,您将优先考虑
params[:page]=“all”
条件,然后是
params[:search]
。在我上面提到的代码中,优先考虑
搜索
,然后是
页面
,但你明白了,对吗

现在,让我们在控制器中添加用户特定的params方法:

def user_params
  params.slice(:brand, :page, :state).merge!({sort_column: sort_column, sort_direction: sort_direction, per_page: per_page })
end
然而,在Rails 4中,使用强参数更容易,例如:
params.require(:user).permit(:search,…)

现在,控制器的索引方法可以如下所示:

  def index
    if params[:brand]
      @users = User.includes(:brand).where(brand_id: params[:brand]).order("#{sort_column} #{sort_direction}").page(params[:page]).per(10)
    elsif params[:search]
      @user = User.includes(:brand).find_by_client_code(params[:search])
      redirect_to edit_user_path(@user)
    elsif params[:page] == 'all'
      @users = User.includes(:brand).order("#{sort_column} #{sort_direction}").all
    elsif params[:state]
      @users = User.includes(:brand).where(state: params[:state]).page(params[:page]).per(10)
    else
      @users = User.includes(:brand).order("#{sort_column} #{sort_direction}").page(params[:page]).per(10)
    end
  end
def index
  if params[:search].present?
    @user = User.find_by_client_code(params[:search])
    redirect_to edit_user_path(@user)
  else
    @users = User.fetch_with_brand(user_params)
  end
end
或者,如果您倾向于将用户重定向到更多位置编辑页面,则可以进一步重构该页面:

before_filter :redirect_to_edit, only: [:index, :some_other_method_name]

def index
  @users = User.fetch_with_brand(user_params)
end

def redirect_to_edit
  if params[:search].present?
    @user = User.find_by_client_code(params[:search])
    redirect_to edit_user_path(@user)
  end
end

你现在有了你的瘦控制器。

我刚刚走了同样的学习道路。我以前将所有AJAX请求放入模板的操作中,但现在将它们放入自己的方法中。我在网上找不到很多关于这个话题的指导。我希望更有经验的人会加入进来,但我将大多数AJAX请求分离为它们自己的操作和路由,并对结果感到满意。逻辑更容易理解,我的行动也不会杂乱无章。我的routes文件填充得更多,但通过适当的组织,它看起来仍然很干净。好问题!您使用的是哪个版本的ruby?我使用的是ruby
2.1.2
。我也没有看到响应块。你是用HTML还是js格式请求这个?对不起,是的,它被删除了。哇,谢谢你的详细回答。好多了!