I am attempting to create a search form, the search is performed on third party site so there is no model just a controller. I have it set up as follows
views/items/index.html.haml :
= search_field :search, :search_input
= submit_tag 'Search'
views/items/search.html.haml:
-#items.each do |item|
%h1= item.title
controllers/items_controller.rb:
def index
#I am unsure of what to put in here? I think
#I need something wich sends #search_input to my search method
end
def search
#items = some_third_party_search_method_i_wrote{ params[:search_input]}
end
How does one properly use the params object in rails? I don't understand how to get from the index page containing the search form to the search page containing the results of the search input?
You probably want to use search_field_tag instead since your form isn't tied to a model/object:
= search_field_tag :search_input
Then params[:search_input] should work.
Related
I have a search function that searches a list of items. My search returns results from a simple GET request. On the page, I would like to display to the user their query. In my controller I can retrieve their query like this:
def search
#query = params[:query]
end
In my view, if I simply place <%= #query %> in the HTML this would possibly create a XSS vulnerability. How can I sanitize this parameter for use in a view?
Is this what you need? https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
<%= sanitize(#query) %>
I'm pretty new to rails and I'd like to set my links for a certain page dynamically. I have a table called "Unfinished" and it has a column called "link" (corrected from "links") I'd like to be able to call the "link" record in the view to set my link_to link path.
I am trying to do this...
<%= link_to #unfinished.link(:p => #post.id) do %> FINISH <% end %>
...but that's not working.
my controller says:
def show
#post = Post.find(params[:id])
#unfinished = Unfinished.where('progress = ?', #post.progress).last
end
and the controller logic works fine...until I try to put the #unfinished.link into link_to
Edit:
Error Message:
wrong number of arguments (1 for 0)
Model
class Unfinished < ActiveRecord::Base
end
The type of links are :
step1_path
step2_path
step3_path
I am making a multipage form that you can save partway through. Based on a value in the #post.progress column (like 1, 2, 3) the correct path to complete the post will be provided (step1_path, step2_path etc...)
try this.
<%= link_to eval(#unfinished.link.to_s) do %> FINISH <% end %>
since the link you want is actually a named route, so you will need to eval it.
but with this you wouldn't be able to be able to pass in the post id, which you will need.
If the route is the same for all records (save for what part you are on based on the progress attribute) do you even need to store it in the database? You could just make the link method return the path (that you would still need to eval).
something like
def link (post)
"step#{self.progress}_path(post.id)"
end
and then eval the link on the way back. but Not sure if that will work, just thinking out loud...
There are gems that do multi-stage forms perhaps looking into them might help?
I have a row of cities at the top of a layout. I also have a sidebar with a list of categories. In a view that is rendered within this layout, I'd like to query results of a model with category and location fields, based on which city has been selected, and by which category is most recently clicked.
I'm having trouble figuring out the best way to do this. I guess I'd like the city buttons to function like radio buttons, so that when a category is clicked, it is still using the last selected location to query the results. I'm also not sure how to pass the clicked button's value to the Offers model's search function:
def self.search(query, query2, query3)
where("title like ?", "%#{query}%").
where("category = ?", "%#{query2}%").
where("city = ?", "%#{query3}%")
end
This is the last step in finishing a large project for me, and any help would be greatly appreciated. If all else fails, I'll just end up using two dropdowns for category and location, but I'd really like to get this working.
Routing
You'll basically need to define a route to handle this - allowing you to pass a series of parameters through your links to your controller, allowing you bring back the data as defined by the links
Whether you handle this through plain HTTP, or through Javascript, it's the same process link > controller > response
I would do this:
#config/routes.rb
resources :offers do
collection do
get :search
end
end
This will allow you to create your links as follows:
<% #cities.each do |city| %>
<%= link_to city.name, offers_search_path(city: city.name) %>
<% end %>
This should create the link: domain.com/offers/search?city=scarborough
--
Controller
When you have a link like the one above, your controller will then be able to handle the city param, allowing you to perform a conditional search as follows:
#app/controllers/offers_controller.rb
Class OffersController < ApplicationController
def search
#result = Offer.search params[:city] #-> considering you want to use a class method
end
end
--
Ajax
If you wanted to perform the search via ajax, you'd do all of the above except you'd handle the link as a remote one:
<%= link_to city.name, offers_search_path(city: city.name), remote: true %>
This will send an ajaxified request to your controller, which you'll be able to handle as follows:
#app/controllers/offers_controller.rb
Class OffersController < ApplicationController
respond_to :js, :json, :html
def search
#result = Offer.search params[:city]
respond_with #result
end
end
I am working a simple rails app and i would like to know how possible it is to use one search form to search inside multiple models. like i have a story model and a book model. this search form should be able to search the both models with a single parameter.
<%= for_tag :url => search_path %>
<%= text_field_tag :q %>
<% end %>
How can i make this search from work for multipple models
Whatever search you need to do, is done inside an action in a controller. You could basically create a controller, say search_controller and have an action say, item
def item
if params[:q]
#found_stories = Story.find_all_by_...(params[:q])
#found_books= Book.find_all_by_...(params[:q])
end
end
Then you could use the objects #found_stories and #found_books in your view to show the search results.
This is just an example of how you could do to fulfill your requirement.
Thanks.
I built a basic search form that queries one column in one table of my app. I followed episode 37 Railscast: http://railscasts.com/episodes/37-simple-search-form
Here's my problem. I want to display the search query that the user makes in the view that displays the search results. In my app, the search queries the zip code column of my profile model, and returns a list of profiles that contain the right zip code. On the top of the list of profiles returned from the search, I want it to say "Profiles located in [zip code that was queried]."
I'm sure I can do this because the queried zip code gets passed into the url displaying the results. So if the url can pick it up, there must be some way to display it in the view on the page as well. But I don't how.
Please keep in mind that I'm not using any search pluggins and I don't want to use any for now. This is my first app, so I don't want to add complexity where it's not needed.
Per Ryan's instructions in the Railscast, here's my setup:
PROFILES CONTROLLER
def index
#profiles = Profile.search(params[:search])
end
PROFILE MODEL
def self.search(search)
if search
find(:all, :conditions => ['zip LIKE ?', "%#{search}%"])
else
find(:all)
end
end
PROFILE/INDEX.HTML.ERB
<% form_tag ('/profiles', :method => :get) do %>
<%= text_field_tag :search, params[:search], :maxlength => 5 %>
<%= submit_tag "Go", :name => nil %>
<% end %>
The search itself is working perfectly, so that's not an issue. I just need to know how to display the queried zip code in the view displaying the results.
Thanks!
Just set it to an instance variable and use that.
def index
#search = params[:search]
#profiles = Profile.search(#search)
end
In your view, you can reference #search.
Also, as a friendly tip, please use an indent of 2 spaces for Rails code. It's the standard way to do it, and others who are reading your code will appreciate it.