Reputation: 27242
How do I set a boolean to false in a method?
For my BusinessStore search I am trying to make 3 different radio buttons to choose from for Online Stores, Offline Stores and All Stores.
This my BusinessStore
model and the 3 methods I am trying to make for the radio buttons:
attr_accessible :online_store # boolean attribute
def online_search
business_store.online_store = true
end
def offline_search
business_store.online_store = false
end
def all_search
# Give all results whether true or false
end
How do I finish this and what needs to be corrected?
UPDATE
Product.rb
def search_type=(boolean)
case (boolean)
when 'online'
@online_search = true
@offline_search = false
when 'offline'
@online_search = false
@offline_search = true
when 'all'
@online_search = true
@offline_search = true
else
@online_search = true
@offline_search = true
end
end
search/index.html.erb
<%= label_tag :search_type, "All" %>
<%= radio_button_tag :search_type, "all" %>
<%= label_tag :search_type, "Online" %>
<%= radio_button_tag :search_type, "online" %>
<%= label_tag :search_type, "Offline" %>
<%= radio_button_tag :search_type, "offline" %>
Upvotes: 0
Views: 329
Reputation: 27242
Took a different approach instead and just made two checkboxes:
SearchController.rb
def index
@search = Product.search do
q.with(:online_search, params[:online_search] == 1) if params[:online_search].nil?
q.with(:offline_search, params[:offline_search] == 0) if params[:offline_search].nil?
end
@products = @search.results
end
search/index.html.erb
<%= label_tag :online_search, 'Online' %>
<%= check_box_tag :online_search, params[:online_search], true %>
<%= label_tag :offline_search, 'Offline' %>
<%= check_box_tag :offline_search, params[:offline_search], true %>
I set the checkboxes to start out as already checked for both in order to search online and offline unless one of them is unchecked. This is why in the search controller I search for the params being nil or not for both methods.
Upvotes: 0
Reputation: 211740
You'd probably have an easier time making a wrapper method that interprets the selection instead of having direct-mapped mutators:
def search_method=(value)
case (value)
when 'online'
@online_search = true
@offline_search = false
when 'offline'
@online_search = false
@offline_search = true
else
@online_search = true
@offline_search = true
end
end
def online_search?
@online_search
end
def offline_search?
@offline_search
end
Then you make a selection between online
, offline
and all
or whatever default you'd prefer.
Edit: Amended based on gist:
def index
@search = Product.search do
fulltext params[:search]
paginate(:per_page => 10, :page => params[:page])
order_by(:purchase_date, :desc)
order_by(:price,:asc)
includes(:business_store)
where(:business_store => { :online_store => true })
end
@products = @search.results
end
Upvotes: 1