Miguel A.
2012-Apr-18  11:12 UTC
RoR - Search Forms with Checkboxes, display appropriate results
I have a form that allows me to search a hotel by city or name and by
rating through radio buttons. It all works.
However, my hotel model has_one :facility, this facility model is
composed by several boolean fields (for example, roomservice:boolean
restaurant:boolean and so on)
The question is, I want to add checkbox fields for each facility I have
and in the search form, when the user selects it and presses Search, it
should only display the hotels which have those facilities. I''ve
searched but i can''t put it to work.
My hotel model:
def self.search(params)
    if params
      arel = where(''city LIKE ? OR name LIKE ?'',
"%#{params[:search]}%",
"%#{params[:search]}%")
      arel = arel.where(''rating = ?'', params[:rating]) if
params[:rating].present?
       (what to put here?)
      arel
    else
      all
    end
  end
My index view:
<%= form_tag hotels_path, :method =>''get'' do%>
        <p>
            <b>Location:</b>
            <%= text_field_tag :search, params[:search]%><br />
            <b>Rating:</b><br />
            <%= radio_button_tag :rating, ''5''%>5
Stars<br />
            <%= radio_button_tag :rating, ''4''%>4
Stars<br />
            <%= radio_button_tag :rating, ''3''%>3
Stars<br />
            <%= radio_button_tag :rating,
''6''%>Hostels<br />
            <b>Facilities:</b>
            (what to put here?)
            <%= submit_tag "Search", :name => nil%>
        </p>
        <%end%>
...
<% @hotels.each do |hotel|%>
<div id="name">
 <h2> <%= link_to hotel.name, hotel  %> </h2>
</div>
   <div id="description">
       <%= truncate(hotel.description, :length => 20) %>
   </div>
<%end%>
Thanks in advance, I can''t comprehend how to it with checkboxes when
they can have multiple results.
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
aash dhariya
2012-Apr-18  11:59 UTC
Re: RoR - Search Forms with Checkboxes, display appropriate results
Maybe this could be your answer:
def self.search(params)
   if params
      query = "SELECT * FROM Hotel where city LIKE
''#{params[:search]}'' OR
name LIKE ''#{params[:search]}''"
      query+= "AND rating = #{params[:rating]}" if params[:rating]
      @hotels = find_by_sql(query)
     arel
   else
     @hotels = Hotel.all
   end
 end
view:
<%= form_tag hotels_path, :method =>''get'' do%>
       <p>
           <b>Location:</b>
           <%= text_field_tag :search, params[:search]%><br />
           <b>Rating:</b><br />
           <%= radio_button_tag :rating, ''5''%>5
Stars<br />
           <%= radio_button_tag :rating, ''4''%>4
Stars<br />
           <%= radio_button_tag :rating, ''3''%>3
Stars<br />
           <%= radio_button_tag :rating,
''6''%>Hostels<br />
           <%= submit_tag "Search", :name => nil%>
       </p>
  <%end%>
  <% @hotels.each do |hotel| %>
     <p>hotel.name</p>
      <p>facilities</p>
     <% hotel.facility do |facility|%>
         <!-- likewise show your facility using facility.[name] over
here-->
      <% end %>
<% end %>
On Wed, Apr 18, 2012 at 4:42 PM, Miguel A.
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:
> I have a form that allows me to search a hotel by city or name and by
> rating through radio buttons. It all works.
>
> However, my hotel model has_one :facility, this facility model is
> composed by several boolean fields (for example, roomservice:boolean
> restaurant:boolean and so on)
>
> The question is, I want to add checkbox fields for each facility I have
> and in the search form, when the user selects it and presses Search, it
> should only display the hotels which have those facilities. I''ve
> searched but i can''t put it to work.
>
> My hotel model:
>
> def self.search(params)
>
>    if params
>      arel = where(''city LIKE ? OR name LIKE ?'',
"%#{params[:search]}%",
> "%#{params[:search]}%")
>      arel = arel.where(''rating = ?'', params[:rating]) if
> params[:rating].present?
>       (what to put here?)
>      arel
>    else
>      all
>    end
>  end
> My index view:
>
> <%= form_tag hotels_path, :method =>''get'' do%>
>        <p>
>            <b>Location:</b>
>            <%= text_field_tag :search, params[:search]%><br />
>
>
>            <b>Rating:</b><br />
>            <%= radio_button_tag :rating, ''5''%>5
Stars<br />
>            <%= radio_button_tag :rating, ''4''%>4
Stars<br />
>            <%= radio_button_tag :rating, ''3''%>3
Stars<br />
>            <%= radio_button_tag :rating,
''6''%>Hostels<br />
>
>            <b>Facilities:</b>
>            (what to put here?)
>
>            <%= submit_tag "Search", :name => nil%>
>        </p>
>        <%end%>
> ...
> <% @hotels.each do |hotel|%>
>
> <div id="name">
>  <h2> <%= link_to hotel.name, hotel  %> </h2>
> </div>
>
>
>   <div id="description">
>
>       <%= truncate(hotel.description, :length => 20) %>
>   </div>
>
>
> <%end%>
>
> Thanks in advance, I can''t comprehend how to it with checkboxes
when
> they can have multiple results.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Miguel A.
2012-Apr-18  14:37 UTC
Re: RoR - Search Forms with Checkboxes, display appropriate results
aash dhariya wrote in post #1057118:> Maybe this could be your answer: > > def self.search(params) > if params > query = "SELECT * FROM Hotel where city LIKE ''#{params[:search]}'' > OR > name LIKE ''#{params[:search]}''" > query+= "AND rating = #{params[:rating]}" if params[:rating] > @hotels = find_by_sql(query) > arel > else > @hotels = Hotel.all > end > endUnforunatly the code above does not work. Its a logical error. And:> <% @hotels.each do |hotel| %> > <p>hotel.name</p>Isn''t showing any hotels at all, even with the search empty. All I altered from your code was>query = "SELECT * FROM Hotelto query = "SELECT * FROM hotels which is the name of the table. If anyone has anymore tips, I would really appreciate it. Best Regards -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.