Created
December 10, 2012 02:05
-
-
Save ohayon/4247981 to your computer and use it in GitHub Desktop.
index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Dec 10, 2012
- 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])
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
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
You don't actually need the has_key? check in this case
lgtm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment