简体   繁体   English

has_scope 按成本特征过滤 [Ruby]

[英]has_scope filter by cost feature [Ruby]

I need to set up a feature where the index page of freelancers can be filtered to only display based on a range on the attribute of price.我需要设置一个功能,可以过滤自由职业者的索引页面以仅根据价格属性的范围显示。 This needs to be done from the user side, i was thinking two input boxes and a submit button.这需要从用户端完成,我在想两个输入框和一个提交按钮。

How do I go about doing this?我该怎么做呢? I checked out the has_scope gem, but I'm worried it's going to mess up my already defined order of freelancers, where they are ordered by the attribute featured (boolean).我检查了has_scope gem,但我担心它会打乱我已经定义的自由职业者的顺序,他们按特性(布尔值)排序。 Essentially the order of the freelancers must stay the same, but then a filter must be added to that ordered object, to only show the freelancers in that price range.本质上,自由职业者的顺序必须保持不变,但是必须在该有序对象中添加一个过滤器,以仅显示该价格范围内的自由职业者。

There must be a way to do this but I'm very unsure.必须有办法做到这一点,但我很不确定。 I'm going to do a bit of a code dump below, can someone point me in the right direction and show me how?我将在下面做一些代码转储,有人能指出我正确的方向并告诉我怎么做吗?

class HomeController < ApplicationController
  before_action :set_freelancer, only: %i[ show edit update destroy ]

  def index
    @pagy, @freelancers = pagy(Freelancer.order(featured: :desc))
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_freelancer
    @freelancer = Freelancer.find(params[:id])
  end
end

My home controller我的家庭控制器

<% if user_signed_in?%>
<%== pagy_nav(@pagy)%>
<div class="d-flex flex-wrap justify-content flex-shrink-1 gap-3">
  <%@freelancers.each do |freelancer| %>
    <div class="card mb-3" style="max-width: 540px">
      <div class="row g-0">
        <div class="col-md-4">
        <img src="https://i.pinimg.com/originals/57/f5/da/57f5da08bd8f52bc2d3e4ebadd67b642.jpg" class="img-fluid rounded-start" alt="6-logo">
        </div>
          <div class="col-md-8">
            <div class="card-body">
              <%= render freelancer %>
              <%= link_to "Show this freelancer", freelancer, class: "btn btn-info" %>
            </div>
          </div>
    </div>
  <br/>
</div>
<%end%>
<% else %>
<h1>Please sign up, or sign in!</h1>
<%end%>

My webpage outputting each freelancer, ive also added my github itself below, im aware ive asked alot for help lately here but i am learning alot so i appreciate it我的网页输出每个自由职业者,我还在下面添加了我的 github 本身,我知道我最近在这里寻求了很多帮助,但我学到了很多东西,所以我很感激

https://github.com/LCzoboriek/freelancer-assignment https://github.com/LCzoboriek/freelancer-assignment

(UPDATE) (更新)

So ive now added this code below to my home_controller.rb所以我现在将下面的代码添加到我的 home_controller.rb

def index
    freelancers_scope = Freelancer.order(featured: :desc)
    freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_lower_than]) if params[:cost_greater_than].present?
    freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?

    @pagy, @freelancers = pagy(freelancers_scope)
  end

and this to my views, but its throwing an error saying undefined local variable or method `index' for #ActionView::Base:0x00000000013178.这是我的观点,但它抛出一个错误,指出#ActionView::Base:0x00000000013178 的未定义局部变量或方法“索引”。 How do i point my form to that freelancers_scope part in my controller?如何将表单指向控制器中的 freelancers_scope 部分?

<%= form_for(index) do |f| %>
  <%= f.input :cost_greater_than%>
  <%= f.input :cost_lower_than%>
  <%= submit_tag("Submit") %>
<% end %>

i was thinking two input boxs and a submit button.我在想两个输入框和一个提交按钮。

this sounds absolutely fine这听起来很好

this is a very basic thing and you don't really need a gem for that, how about:这是一个非常基本的东西,你真的不需要宝石,怎么样:

  def index
    freelancers_scope = Freelancer.order(featured: :desc)
    freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_greater_than]) if params[:cost_greater_than].present?
    freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?

    @pagy, @freelancers = pagy(freelancers_scope)
  end

if you really want a scope you could later define it in the Freelancer model so you wouldn't have to call where directly in the controller如果你真的想要一个范围,你可以稍后在Freelancer模型中定义它,这样你就不必直接在控制器中调用where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM