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
Related
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.
I want to set a :data attr if a condition is met. In this case a user should have a role. So it's a boolean statement. Let me give an example.
- #data = 'contract-confirm'
.create_button= link_to 'Something', new_some_path(#customer), :class => 'btn btn-success', :'data' => #data ? 'contract' : nil
.clearer
So I know this might look strange but I want to set a data attribute if customer is labeled and then hook js to that data attr. That part works. What does not work is that now I'm setting the attribute always. So even in the case that customer does not have the role the js gets hooked. I know that I am not explicitly indicating at all the role here. I have a #customer.role? but I cant seem to incorporate it properply. I managed it before with an if else statement but then with a lot of duplication which I'm not so fund of. Any tips?
You can try this piece of code:
link_to 'Something', ... , :data => #customer.role? ? 'contract' : nil
haml won't include nil attributes, so it should work as you expect.
Try to replace :'data' => #data ? 'contract' : nil with :'data' => 'contract' if #data.
I checked it and next code:
- #data_present = true
= link_to 'Something', root_path, :class => 'btn', :'data' => ('test' if #data_present)
renders to:
Something
And code without - #data_present = true renders to:
Something
I know that link_to uses get action by default and also you can change the method to post by passing :method => :post to link_to function, but it does not seem to work. Here is the syntax that I am using:
= link_to "Export" export_path(:data_array => d_array), :method => 'post'
But this does not seem to work. The array is being passed by as a query parameter which I can see in the URL box and it bombs my application since it blows the string length limit in the url string.
Try using form instead:
= form_tag export_path do
- d_array.each do |val|
= hidden_field_tag 'data_array[]', val
= submit_tag 'Export'
Notice that in a controller params[:data_array] will be an array of strings.
For example I have this code:
<%= link_to "Start", start_path(:id=>1,:box=>1)%>
the id and the box are parameters right?
and for example, it generated this url: http://localhost:3000/start?id=1&box=1
How can I add a string to it, to make look like this:
http://localhost:3000/start?id=1&box=1#box_1
Use the :anchor key:
<%= link_to "Start", start_path(:id => 1, :box => 1, :anchor => 'box_1')
And to answer your first question, yes, id and box are parameters. They are passed in to the request as part of the params hash.
In my Rails app, I need to set a value for a custom data-* attribute on an anchor tag. However, hashes can't have hyphens if I'm not mistaken. I essentially want to do this:
<%= link_to 'Example', example_path, :class => 'something', :data-id => '15' %>
:data-id is not valid, however. What can I do to work around this?
IIRC, for such purposes hashes and strings are equivalent, so you can use "data-id" instead of :data-id. Never checked for this particular method, though, so no guarantees.
I think in Rails 3 you can do
link_to "Click Here", root_path, :data => { :id => #model.id }
for all data attributes.