开发者

Rails doesn't handle params

I'm trying to implement advanced search.

I have this in the model:

def self.filter(params)
params.inject(self) do |scope, (key, value)|
  return scope if value.blank?
  case key
    when :min_size
      scope.scoped(:conditions => ["size >= ?", value])
    when :max_size
      scope.scoped(:conditions => ["size <= ?", value])
    else
      scope.scoped(:conditions => ["created_at > ?", 4.weeks.ago]) 
  end
end
end

This is in my controller:

  def search
    @listings = Listing.filter(params)
    respond_to (:html)
  end

and I search with this form:

<%= form_tag '/search', :method => "get" do |f|  %>
    <%= text_field_tag :min_size, params[:min_size] %>
    <%= text_field_tag :max_size, params[:max_size] %>
    <%= submit_tag "Search", :name => nil %>
<% end %>

When I'm trying to search Rails seems to ignore all the params passed and finds all the entries with the last case switch ("created_at > ?", 4.weeks.ago). Params, however, do 开发者_StackOverflowpass:

 Started GET "/search?utf8=%E2%9C%93&min_rent=10&max_rent=35" for 127.0.0.1 at Mon Jan 31 12:25:40 +0500 2011
 Processing by ListingsController#search as HTML
 Parameters: {"utf8"=>"✓", "min_size"=>"40", "max_size"=>"50"}
 Listing Load (1.1ms)  SELECT "listings".* FROM "listings" WHERE (created_at > '2011-01-03 07:25:40.978742') AND (created_at > '2011-01-03 07:25:40.980977') AND (created_at > '2011-01-03 07:25:40.982080') AND (created_at > '2011-01-03 07:25:40.983077') AND (created_at > '2011-01-03 07:25:40.984138')

What might be the problem here? In console it works just fine

>> some = Listing.filter(:min_size => 40, :max_size => 50)
=> [#<Listing id: 1, title: "1 br apartment, Paris 16e", address: "1 Rue Monsieur-le-Prince", district: "Paris 6e", size: 42, rent: 2000, created_at: "2011-01-26 18:30:57", updated_at: "2011-01-26 19:00:44">]


I'm guessing the problem is switch-case comparison:

case key

I think from the inject the key is "min_size" not :min_size and that's why it always goes to else branch.

EDIT:

Try changing the code as

when "min_size"
  scope.scoped(:conditions => ["size >= ?", value])
when "max_size"
  scope.scoped(:conditions => ["size <= ?", value])

and it should work. Maybe :)


You can accomplish this using the Searchlogic gem.

with Searchlogic you can do a greater than/less than search like this:

irb> Listing.search(:size_gt => 10, :size_lt => 100)

in your controller:

def index
  @search = Listing.search(params[:search])
end

in your view:

<%= form_for @search do |f|  %>
  <%= f.text_field :size_gt %>
  <%= f.text_field :size_lt %>
  <%= f.submit "Search" %>
<% end %>

urls look like this

/listings?search[size_gt]=10&search[size_lt]=100
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜