简体   繁体   中英

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

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

(UPDATE)

So ive now added this code below to my 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. How do i point my form to that freelancers_scope part in my controller?

<%= 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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