New to rails and building my first app on my own (did a few tutorials). Wanting to build an app where the root page, search.html.erb view, is only a search bar with two params, and outputs the search result to the index view. Thanks in advance!
What I have now:
Controller:
def search
#tours=Tour.search(params[:date], params[:region])
end
Search View:
<%= form_tag tours_path, :method => 'get' do %>
<p>
<%= text_field_tag :region %>
<%= text_field_tag :date %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Tour Model:
def self.search(date, region)
if date && region
where(date: date, region: region)
else
all
end
end
Routes:
get '' => 'tours#search'
post '' => 'tours#search_results'
get 'tours/:date/:region' => 'tours#index'
resources :tours, except: :index
Rake routes:
root GET / tours#search
GET / tours#search
POST / tours#search_results
GET /tours/:date/:region(.:format) tours#index
Related
Listing model code
def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['franchisename LIKE ? or longdescription LIKE ?', search_condition, search_condition])
end
Home controller
def search
#results = Listing.search params[:search]
end
Route
get 'home/search'
Home/index.html.erb search form
<%= form_tag :controller => 'home', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'indexsearch' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Home/search.html.erb search results
<%= #results.each do |result| %>
<%= result.franchisename %>
<%= result.shortdescription %>
<% end %>
Error: No route matches [POST] "/home/search"
I just want it so that I can type in my search query on the home page and then upon submission it redirects to a results page containing the results. I followed a guide initially but maybe it was outdated, though it doesn't say. Would appreciate some help!
Just defined the form_tag in the following way.
<%= form_tag url_for(:controller => 'home', :action => 'search'), :method => 'get' do %>
Hope your problem will be resolved!
Probably you should specify controller and action in to your routes like below:
get 'home/search' => 'home#search'
Hope this resolve your problem!!!
Cheers!!!
I want to search text from mysql db table .
view page:
<% form_tag subjects_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
controller code:
before_action :confirm_logged_in
def index
#subjects=Subject.sorted
#subjects = Subject.search params[:search]
end'
Routes used:
root 'demo#index'
get 'admin', :to=> "access#index"
match ':controller(/:action(/:id))' ,:via=>[:get,:post]
i am waiting for your answer.
in routes.rb you need to define the subjects
resources :subjects, only: [:index]
You've not specified any named routes so subjects_path doesn't exist.
I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params
I am working on rails app , In which I have created a table Product Name:string and Number: integer.
The application should give user a form where he can search a product by his number if product exists it should give product name from database.
My search.html.erb is this.
<%= form_for #products, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_area :number, :size => "60x12" %>
<%= f.submit "Search" %>
<% end
What will be the definition of search Method in ProductController and routes i need to add in routes.rb?
Irrespective of nifty forms, this is how I would have done this:
In config/routes.rb
replace resources :products' with
resources :products do
post 'search', :on => :collection
end
This will give me a search_products_path
In your view:
<%= form_for(:search, :url => search_products_path) do |f| %>
<%= f.text_field :number, :placeholder => "Enter number to search" %>
<%= f.submit "Search" %>
<% end %>
In your products_controller.rb
def search
number = params[:search][:number]
#result = Product.find_by_number(number)
#not_found = true unless #result
end
In your views/products/search.html.erb, use #result to show the product information; take care while checking whether or not the desired product is actually found or not. I have set the boolean #not_found in case it doesn't exist.
I have a controller games and a method:
def index
#games = Game.all
end
def set_game
#current_game = Game.find(params[:set_game])
end
In my view I have:
<%= form_tag("/games") do %>
<% #games.each do |g| %>
<%= radio_button_tag(:game_id, g.id) %>
<%= label_tag(:game_name, g.name) %><br>
<% end %>
<%= submit_tag "Confirm" %>
<% end %>
Routes:
resources :games
match 'games', :to => 'game#index'
How can I make this form work for my set_game method?
Thanks.
<%= form_tag(set_game_games_path) do %>
...
<% end %>
#routes.rb
resources :games do
collection do
get '/set_game', :as => :set_game
end
end
That's an example of a custom route:
match "customroute" => "controller#action", :as => "customroutename"
This can be then accessed with "customroutename_url" in your views. If you want to create a custom route for your set_game action, for example, it would be
match "setgame" => "games#set_game", :as => "setgame"
Then you can do
<%= form_tag setgame_url %>
...
<% end %>
You can read more about custom routes here: http://guides.rubyonrails.org/routing.html#non-resourceful-routes