Rails: filter index page and redistributions of filter inputs

I'm not sure if this is the correct way to Rails 3. I want to have filter criteria fields at the top of my index view that will filter the data.

What I have below works to filter my list, but I also want the filter fields to be re-populated using the current filter, and I cannot figure out how to do this. The method I got below also doesn't look like the โ€œrightโ€ way to do this, since it relies on things like passing an empty string to auxiliary field methods in my opinion.

Controller:

def index @animals = Animal.by_color(params[':color']).by_age(params[':age']).paginate(:page => params[:page]) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @animals } end 

end

View:

 <h1>Listing Animals</h1> Filter By: <%= form_for(:animal, :url => { :action => "index" }, :html => {:method => :get} ) do |f| %> <div> Color: <%= text_field'', ':color', :size => 10 ) %> Age: <%= text_field('', ':age') %> <%= f.submit "Filter List" %> </div> <% end %> <%= will_paginate %> ... 

I use scope methods in my model that work very smoothly. I'm just vague about how the params method of the controller and the View methods "form_for", "text_field" match each other. Any ideas on how to reorganize this, as well as get the filter fields currently filled in while filtering?

Decision

Controller:

 def index @search = Animal.new(params[:animal]) @animals = Animal.by_color(@search.color)... .... 

View:

 <%= form_for(@search, :url => { :action => "index" }, :html => {:method => :get} ) do |f| %> <%= f.text_field(:color) 

The population of the form works by creating an @search object in my contorller (in addition to my main animal object). Then use form_for to create around it.

+6
source share
2 answers

What you need is a "request by example", which will work in the controller in approximately the same way:

 @filter = Animal.new(params[:filter]) @animals = @filter.get_scope.paginate(:page => params[:page]) 

and in the controller you can do form_for in this example entry, but I donโ€™t know about any modern Rails plugins that do exactly that. Of course, there should be something like this, but look carefully. I bet you could write something like this in a couple of hours (just grab the hash of the attributes of your filter entry in :conditions area, roughly, like this - NOT TESTED THOUGHTS):

  def get_scope # remove all nill attrs non_default_attrs = self.attributes self.columns.each do | col | # Ignore columns that have default values non_default_attrs.delete(col.name) if non_default_attrs[col.name] == column.default # Ignore columns whose values are nils non_default_attrs.delete(col.name) if non_default_attrs[col.name].nil? end where(non_default_attrs.symbolize_keys) end 
+2
source

controller

 @search = params[:animal] @animals = Animal.by_color(params[:color]).by_age(params[:age]).paginate(:page => params[:page]) 

view

 <h1>Listing Animals</h1> Filter By: <%= form_tag animals_path do %> <div> Color: <%= text_field_tag 'color', :size => 10 %> Age: <%= text_field_tag 'age' %> <%= submit_tag "Filter List" %> </div> <% end %> <%= will_paginate %> 
-1
source

Source: https://habr.com/ru/post/894053/


All Articles