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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Aash dhariya at Apr 18, 2012 at 11:59 am
    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. 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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Miguel A. at Apr 18, 2012 at 2:37 pm

    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
    end
    Unforunatly 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 Hotel
    to
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 18, '12 at 11:12a
activeApr 18, '12 at 2:37p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Miguel A.: 2 posts Aash dhariya: 1 post

People

Translate

site design / logo © 2022 Grokbase