Save Ruby On Rails Search Form Data

Trying to create a search on my homepage using simple_form (almost the same as formtastic). The search works fine, and I get my results, but after submitting, I want to save the values ​​with what the user submitted.

I use a namespace for my form, so how to save data for the form. Here is some code that might help.

controller

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

View

%h1 Search Form
= simple_form_for(:search) do |f|
    = f.input :location, :as => :select, :collection => Location.all.asc(:name)
        = f.input :type, :collection => PropertyType.all.asc(:name)
        = f.input :bedrooms, :collection => 1..10,
    %p.box
        = f.button :submit

-if @results
    %h1 Search Results
    .results
        - @results.each do |property|
            .result
                %h1= property.title

In the Index controller, I tried all kinds of things, i.e.

@search = params[:search]

But every time I try something, the search is interrupted.

What am I doing wrong?

I hope you can advise

+3
source share
3 answers

One approach is to do what Javier Holt has proposed and pass values ​​to each input. The simple doco form offers:

 = f.input :remember_me, :input_html => { :value => '1' }

, . SimpleForm , - activerecord.

:

class PropertySearchCriteria
  attr_accessor :location, :type, :bedrooms
  def initialize(options)
    self.location = options[:location]
    self.type = options[:bedrooms]
    self.bedrooms = options[:bedrooms]
  end
end

:

def index
  @property_search_criteria = PropertySearchCriteria.new(params[:search])
  @results = Property.search(@property_search_criteria)
end

( Property.search)

simple_form_for:

= simple_form_for(:search, @property_search_criteria) do |f|

, simpleform . , PropertySearchCriteria, simpleform.

, , , , .

+2

:

def index
  @results = Property.search(params[:search])
  store_search
end

def store_search
  session[:search] = params[:search]
end

, ,

...
clear_search if session[:search]

def clear_search
  session[:search] = nil
end
0

- , ( formtastic, -, ). , , @search :

@search = params[:search] || {}

@search[:key] :value ( @search.default = '', ):

<%= text_field_tag :name, :value => @search[:name] %>

, . AJAXy, , , , , , , .

, !

0

All Articles