How to add addition condition to ransack after submiiting the form - ruby-on-rails

I have a book library rails app using ransack. Basically, after searching for book title and category. I want to have additional condition like in Amazon page. So I use link_to with parameter of ransack. But the problem is that the page will render the result will only base on the parameter which I sent in link_to. Is there a way that allow me to insert a condition in current search session of ransack. Thanks in advance.

You'll need to merge the new condition into the existing filter and pass that hash to the path helper.
Something like this:
books_path(q: params[:q].merge(additional_filter_eq: value))

Related

Multiple index actions in Rails

I have a Game model and GamesController. Currently my index page shows first 10 records from the database for the purpose of the application. However, i want to make another page where all of the games are being shown.
My question is, what's the Rails way™ of achieving this purpose? Is it possible to have the index action of my GamesController to make requests based on what URL i want to render? (Something like http:localhost:3000/all)
Rails 3.1 - How do I organize multiple index actions for the same model?
was the closest to my issue, but the question tackles problem from rails 3.1 dating back to 2012.
You can really only have one index action per controller, but there are a couple of ways you can achieve this.
First, simply create a new action and have a separate page. Think of an appropriate name for it and create the controller, route, and view. You can keep the amount of code to a minimum by having much of the view code in a partial and use that in both views.
The other way to do this, if you really only want one action, is to pass a parameter to the index controller and query the database based on that parameter. For instance:
link_to 'link text', game_index_path(:g => 'all')
will create a url like: http://domain.com/game/index?g='all' and in the controller you can do this:
def index
which_games = params[:g] # should be all in this case
#games = Game.where(:criteria => which_games)
end
You can use this same method to implement sorting and filtering and all sorts of things.

Rails - Passing Parameters through link_to

I want to create an Add to Watchlist column + link after the description field ,so the selected movie to be stored on another rb. page from the admin section called "Watchlist".
The concept is similar to a Shopping Cart,passing the data from one page to another.
How's that possible in ActiveAdmin?
I would appreciate your hints and advices.
I'm not familiar with ActiveAdmin, but if your question is how to pass parameters through link_to, try this:
link_to 'Somewhere', some_path(param_1: 'foo', param_2: 'bar')
When you click through, the params should be present in the query string and in the params hash that Rails provides. You can pull out the values with:
params[:param_1]
params[:param_2]

Rails search/checkbox logic

I am trying to create some search function.
I do want users to be able to check checkboxes.
When the form submitted the ID of the companies should be send as params to a given URL.
In my controller i need to create some search action, I just dont know how to params are gonna be.
def search
#companyies = Company.find(find all the companies that match the params)'
end
How should I create the checkbox loop?
How should I create the search action?
Regarding the use of check boxes, this might be of some help to you.
A Railscast by Ryan Bates
Instead of rolling your own search system, you may have better luck using something like searchlogic that does most of this for you.

How can I pass the subdomain as a value to search for a record in Rails 2?

I want to be able to send users to the following styled-URL:
subdomain.mydomain.com/tag
I have a LandingPage record, and so would like to be able to search for the specific LandingPage record by matching the method LandingPage.subdomain with the value in subdomain.
I would then want to store the value found in tag when someone submits a form on that page.
How can I do this without creating in my DNS or in Zerigo the specific subdomain in the URL? I'd like it to respond to a wildcard, but use the value to do a record search of an ActiveRecord.
Ryan does something very similar in his Railcast on subdomains:
http://railscasts.com/episodes/221-subdomains-in-rails-3

Tag chaining in Rails URLs

I want to implement tags in my Rails application but I'm having trouble with one specific feature of that implementation. You see I want to make it possible for users to specify multiple tags in the url scheme, something like http://domain/tags/:id1/:id2/.... Has anyone implemented something like this in Rails or know how to do it?
You want something called "Route Globbing":
match 'tags/*tag_ids' => 'tags#lots_of_tags'
Then in lots_of_tags action you'd have params[:tag_ids] array of provided tag ids or a string in form of id1/id2 (not sure which one, you should test it by yourself).
More info on this in rails guides:

Resources