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
Problem Running Thinking Sphinx with Rails 2.3.5
Ruby on Rails Active Admin Has_Many Changing Dropdown to Use a Different Column
Rails 7 Signup Form Doesn't Show Error Messages
Ruby Is Already Using the Class Name of My Model
Bundle Exec Jekyll Serve: Cannot Load Such File
Trouble Installing Ruby 1.9.2 with Rvm MAC Os X
Garbage Collector in Ruby 2.2 Provokes Unexpected Cow
Ruby Equivalent of C#'s 'Yield' Keyword, Or, Creating Sequences Without Preallocating Memory
Ruby: Overriding the Puts Method
How to Install "Readline" for Rails Console
Exec the Cd Command in a Ruby Script
How to Run an Excel MACro from Ruby
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
How to Generate a Unique Request Id in Rails
Ruby Minitest Assert_Output Syntax
Why Does the Break Statement in Ruby Behave Differently When Using Proc.New V. the Ampersand Sign