0

I have a search form and I need a checkbox that will select and then return whether a particular listing allows pets. I have created a custom route, controller method, and erb in the view. However, I am not accomplishing what I set out to do.

When a user clicks the Pets Allowed checkbox and then clicks search, the listings where pets allowed == true should be returned. I am not sure how to go about that.

This is the current code, but does not accomplish what I am after. This will redirect to /pets_allowed but that isn't a real thing.

listings_controller:

def pets_allowed
  @listings = Listing.where(pets: true)
end

routes.rb:

get "pets_allowed" => "listings#pets_allowed"

html.erb:

<div>
  <%= link_to 'Pets Allowed', pets_allowed_path, :class => 'button btn-transparent' %>
</div>
1
  • Can you paste the search form code Commented Dec 4, 2016 at 17:27

1 Answer 1

2

You probably need to use form_for instead of link_to.

<%= form_for :search_pets, url: pets_allowed_path, method: :get do |f| %>
  <%= f.check_box :has_pets %>
<% end %>

Now in action,

def pets_allowed
  @listings = Listing.where(pets: params[:search_pets][:has_pets])
end

When the checkbox is unchecked, it will return all the listings with no pets and when its checked it will return all the listings that have pets.

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.