Your controller is using this method to load results on your index page, whether or not any search was performed. So it's perfectly reasonable for the empty search to return all results.
def self.search(search)
if search
find(:all, :conditions=>['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
end
def index
@disks = Disk.search(params[:search]) # <-- right here
respond_to do |format|
format.html # index.html.erb
format.json { render json: @disks }
end
end
If you kink the self.search method to only return results if there is a non-empty search request, then you will never see anything on your normal index view unless a search request has been entered.
You could kink the form to only submit if there is a value in the search field, you would do that with JavaScript. Something like this (inline) or better you would use Prototype or jQuery to write an unobtrusive handler.
<%= form_tag disks_path, :method => 'get', :onsubmit => "return (this.search.value != '')" do %>
Walter
On Jul 6, 2012, at 2:52 PM, sehrguey o. wrote:
> happy moments are too short...
>
> the search form worked and still works but it has a major flaw - when
> the text_field_tag is left blank then all the db raws pop up on hitting
> submit button.
> How to prevent it?
>
> I tried to substitute 'if (search.present?)' for just 'if search' in
> disk.rb (the googled out trick seemed so logical) yet it availed of
> nothing and all the records were displayed again.
>
> Now what?
>
> yours`
> sehrguey
>
> --
> 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-US.
>
--
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-US.
No comments:
Post a Comment