Home > Software engineering >  has_scope filter by cost feature [Ruby]
has_scope filter by cost feature [Ruby]

Time:06-19

so its me again, i'm totally lost on this one, there's very little guides out there to do this so im hoping someone could guide me along. 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 boxs and a submit button. How the hell do i go about doing this? I checked out the has_scope gem, but im worried its going to mess up my already defined order of freelancers, where they are ordered by the attribute featured (boolean), so 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 im very unsure. Im 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 >
  <%@freelancers.each do |freelancer| %>
    <div  style="max-width: 540px">
      <div >
        <div >
        <img src="https://i.pinimg.com/originals/57/f5/da/57f5da08bd8f52bc2d3e4ebadd67b642.jpg"  alt="6-logo">
        </div>
          <div >
            <div >
              <%= 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 %>

CodePudding user response:

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

  • Related