Add param to an existing URL in Rails 4 link_to - ruby-on-rails

In my rails project I am in this URL, result of a search form:
http://localhost:3000/buscar?localidad=&page=2&&query=de
And I have an (a..z) list to order alphabetically the results. When I press the 'A' button I want to add a parameter to my previous URL, so it'd be like this:
http://localhost:3000/buscar?localidad=&page=2&&query=de&start=A
But in my 'A' button y have this link_to:
= link_to 'A', search_index_path(start: 'A')
so the URL is just:
http://localhost:3000/buscar?start=A
, removing all the previous params I had... :(
How can I do to 'concatenate' the params to the previous params I already had in the URL?
Thanks!

You can do:
= link_to 'A', params.merge(start: 'A')

Related

Rails - Merge Multi-Select Params into comma-separated string

I have a select box that allows multiple values, to filter the results on the page. When I select multiple, the Parameters that are submitted look like this:
Parameters: {"categories"=>["books", "films"], "commit"=>"Submit", "id"=>"87"}
When I am returned to the page, the URL is:
http://localhost:3000/87/projects?categories%5B%5D=books&categories%5B%5D=films&commit=Submit
The URL I would like to return is:
http://localhost:3000/87/projects?categories=books,films
How can I return these params[:categories] as a comma-separated string in the URL? Also, is it possible to remove the "&commit=Submit" from the URL?
Here is my full form code:
<%= form_with url: project_path(#project), local: true, method: :get, skip_enforcing_utf8: true do |form| %>
<%= form.select(:categories, #categories.map {|category| [category.name,category.slug]}, options = { selected: params[:categories], include_blank: "Select Categories", include_hidden: false }, html_options = { multiple: true }) %>
<%= form.submit 'Submit' %>
There's a couple JS & Rails way to do what you want. I can think of a quick and easy one using rails only: Redirecting the URL you are getting to another route with the data parsed as you want it. Like this -->
Assuming this is your route to project_path : get 'project', to: 'project#reroute', as: :project
You can go to your reroute method in the project controller and parse the data you got.
project_controller.rb
class ProjectController < ApplicationController
def reroute
redirect_to your_path(categories: params[:categories].join(','))
end
end
This converts your categories array to a string with your values separated by commas. It is not an array anymore. and it also removes "&commit=Submit" like you wanted.
If you dislike the rails routing method, you can also make your submit button to run some JS functions that builds the url string as you want it. For example <%= submit_tag , :onclick => "return buildUrl();" %>
Having this said, I must say I agree with Edward's comment, the url encoded format is standard and works out of the box, no need for all the additional rerouting and parsing. Im pretty sure whatever you need the data for can be used with the URL encoded format with proper parsing.

How to add a search parameter to an existing Ransack query

I have a search form on a page, that allows a user to filter a list on multiple conditions. Now I'd like to add quick links on top of the list to apply scopes. I tried it like this:
= link_to "Visited", q: {"activated" => true}
While this filters the list to show only activated items, it also resets the search query. In other words, it doesn't remember what was already filtered in the form.
Is there a way to adapt #q so that I can add this "activated" => true to the hash of required filters?
Assuming you're only using the :q param to filter, you could aggregate that.
= link_to "Visited", q: (params[:q] || {}).merge(activated: true)
I don't think you can because if you follow a link you are not submitting the form therefore the parameters are not going to be submitted.
Passing the params in your link to will send the params if any exist:
= link_to "Visited", q: {"activated" => true, "your_params" => params}
This will only work if the form has been submitted once though, otherwise the params would be empty.
EDIT
I assume that the fields on your forms are populating if there is a value.
For example,
<%= text_field_tag(:email, if !params["email"].nil? then params["ip_email"] end) %>

create a link with parameters, but show param only if condition occurs

How can I create a link_to and have a certain param to be in the url only if some condition occurs?
For example, I want to have someurl.com/1111?foo=clown
But the foo should be there only if a condition is true.
If not, it should be just:
someurl.com/1111
<%= link_to 'My Link', your_path(:foo => ("clown" if your_condition)) %>
In this exemple, :param equals to "clown" if your_condition is true, else :param is nil and if the param is nil, Ruby don't consider there is a parameter and ignore it.
Maybe a simple condition like this could work.
if (myCondition)
link_to :myPage, myVariable: var
else
link_to :myPage
end

How can i add parameter to the given url which is dynamic in rails

I have page having url
http://localhost:3000/athletes/list
When user search on this page it changes to something like
http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar
Where first_name and last_name are the search parameters when user search.
I want a link which add the parameter view_type=something in url for ex.
For 1st URL
http://localhost:3000/athletes/list?view_type=something
For 2nd URL
http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar&view_type=something
I have tried following
<%= link_to "Something ", :view_type => 'something' %>
But for both url it gives following url
http://localhost:3000/athletes/list?view_type=something
<%= link_to "Something ", params.merge(:view_type => 'something') %>
Though above code works in most of the cases for some reason it is giving error for some url may be because i am using friendly_url for ex.
for url
http://localhost:3000/athlete/sachin_ramesh_tendulkar_1
giving me following wrong url
http://localhost:3000/athlete/1?view_type=something
To fixed this i am using javascript method as follows
function something(){
var par =""
var link =window.location.href
if (link.indexOf('view_type=something') == -1)
par = link.indexOf('?') != -1 ? "&view_type=something" : "?view_type=something"
window.location.href = link+par
}
and rails code
<%= link_to "Something ", "javascript:void(0)", :onclick => "something();" %>
this might be useful as well (from the API)
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to#343-link-to-some-url-with-current-params

How do I build link with specific part of params hash from previous request?

While I wouldn't normally create a page like this, please know this is a current constraint I can't change.
The page has a checkbox form as well as a table with links for THs that sort the table. I need to construct the TH link in a way that it retains the checkbox items already checked.
Checkbox constructed in View with Haml as:
= form_tag movies_path, :method => :get do
Include:
- #all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", "1", (#ratingsarray.include?(rating) ? true : false)
= hidden_field_tag 'sort', #sort
= submit_tag 'Refresh'
Then for the table it has this for the TH
%th{:class => #classrelease_date}
%a#release_date_header= link_to "Release Date", movies_path(:sort=>'release_date', :params[:ratings]=>params[:ratings])
Ultimately I want the URL like "/moves/?sort=release_date&Ratings[PG]=1&Ratings[G]=1" where I am spitting out the ratings params back to the page as part of the URL. Or how to I pass the ratings params in any part of page where the existing controller code will read it.
Existing controller code access ratings from checkbox:
params[:ratings]
Since movies_path accepts hash as parameter, you can tailor params and then generate the URL with movies_path(params). Generally, you may need to remove "controller" and "action" from params.

Resources