Building a Simple Search Form in Rails

How to build a search form?

Here is mine...

In my Model:

  def self.search(search)
if search
losearch = search.downcase
where('lower(artist) LIKE ?, "%#{search.to_s.downcase}%")
end
end

In my Controller:

def index
if params[:search]
@pins = Pin.search(params[:search])
else
@pins = Pin.paginate(:page => params[:page], :per_page => 50).order("created_at DESC")
end

respond_to do |format|
format.html # index.html.erb
format.json { render json: @pins }
# format.js
end
end

In my view:

<div class="index_search">
<%= form_tag(pins_path, :method => "get", class: "navbar-form", id: "search-form") do %>
<div class="input-append">
<%= text_field_tag :search, params[:search], class: "span2 form-control", placeholder: "Search Reviews" %>
<!-- In order to have the "search" icon int the button, we need to use plain HTML instead
of using a Rails form helper -->
<button class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
<% end %>
</div>
</div>

You'll want to change the "pins_path" above to whichever path you need and the 'lower(artist) LIKE ?' should be 'lower(internship)'

Rails Search Form

Here's a bit of skeleton code to get you started based off what I think you need from what you have said. I hope this is useful.

For the search bit you could do something like this in your index view:

<%= form_for User.new, :url => "search" do |f| %>
<%= f.label :name %>
<%- f.text_field :name %>
<%- end %>

In your controller:

def search
q = params[:user][:name]
@users = User.find(:all, :conditions => ["name LIKE %?%",q])
end

and in your search view:

<%-@users.each do |user| %>
Name: <%=user.name %>

<%- user.achievements.each do |achievement| %>
<%= achievement.name %>
<%- end %>
<%- end %>

You would, of course, need to ensure the users and achievement models are correctly linked:

class User << ActiveRecord::Base
has_many :achievements

end

How to implement simple search form to be used in conjunction with paginate?

Railscast you following is pretty old, lots changed since then. Try to change implementation of search method like this:

def self.search(search)
if search
where 'name LIKE ?', "%#{search}%"
else
scoped
end
end

and something like this in controller:

def index
@projects = Project.search(params[:search]).paginate(:page => params[:page])
end

How to capture a search term from a form and pass it to a method in Rails app

You must set an action to the form and a name to the field

<form class="form-inline my-2 my-lg-0" action="/search" method="get">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search", name: "search>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

Then define the method in a controller.

StaticPagesController

def search
@search_term = params[:search]
end

And define your route in routes.rb

match '/search',  to: 'static_pages#search', via: 'get'

how can I put a simple search form into the application layout in rails 2.3.x?

You should use a SearchController. I use ThinkingSphinx and it works fine, so no code to help there, but using a SearchController will allow you to route that way quite easily.

    <% form_tag search_search_path, :method => :get do %>
<p id="search_header">
<%= text_field_tag 'q', params[:q], {:size => 40} %>
<%= submit_tag 'Search for Shops', :name => nil %>
</p>
<% end %>

Something like this. I called my search method search in the Search controller... that's why the double search in the route.



Related Topics



Leave a reply



Submit