Reputation: 2953
I have a controller action like this
def search
if params[:latitude].present? && params[:longitude].present? && params[:longitude].strip != "" && params[:latitude].strip != ""
room_address = Room.near([params[:latitude], params[:longitude]], 15, order: 'distance')
elsif params[:location].present? && params[:location].strip != ""
room_address = Room.near(params[:location], 15, order: 'distance')
else
return
end
end
As you can see am just checking the presence of params by typing them one by one. Is there any better way??
Upvotes: 0
Views: 505
Reputation: 2303
Maybe something like:
search_param = params[:location] || [params[:latitude], params[:longitude]]
room_address = Room.near(search_param, 15, order: 'distance')
You can also make sure that the params aren't coming in as empty strings by using front end validation instead of using .strip != ''
in your controller.
Upvotes: 3