Skip to content

Instantly share code, notes, and snippets.

@ohayon
Created December 10, 2012 02:05
Show Gist options
  • Save ohayon/4247981 to your computer and use it in GitHub Desktop.
Save ohayon/4247981 to your computer and use it in GitHub Desktop.
index
def index
if params(:state) == used
@coupons = Coupon.all(params[:state])
else
@coupons = Coupon.all
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @coupons }
end
end
@mattcarbone
Copy link

  • syntax on line 2: params is a hash not a method... so access it like you are on line 3
  • line 2: need to check if state is present so it doesn't break when it's not there.
  • line 2: state will be a string so == used won't work
  • to query with state, .all() won't take a param. use Coupon.where(:state => params[:state])

@mattcarbone
Copy link

def index
if params.has_key?(:state) && params[:state] == "used"
@coupons = Coupon.where(:state => params[:state])
else
@coupons = Coupon.all
end

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @coupons }
end

end

@ohayon
Copy link
Author

ohayon commented Dec 10, 2012

  def index 
    if params[:state] == "used"
      @coupons = Coupon.where(:state => params[:state])
    else
      @coupons = Coupon.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @coupons }
    end
  end

@mattcarbone
Copy link

You don't actually need the has_key? check in this case

@mattcarbone
Copy link

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment