I want to update a single attribute via a link (click "set as default account", which sets the is_default column to true). I have the following link:
<% #accounts.each do |account| %>
.
.
<%= link_to 'Set as default', account, method: :put %>
... as you can see I'm using "account" object to set the URL. This results in something like "/accounts/7". But, I don't know how to pass the is_default=true param. Should I do something different here? Should I use a *_path? Also I'm guessing I want an address such as "/accounts/7?is_default=true" (or "/accounts/7/setdefault" and configure the controller and routes.rb to handle this?)
How do I pass a param in a string in this case? Also, what is the best practise? I've looked in other questions but can't quite find something specific to this, then again I am a newbie :(
Thanks
You can do it like this, for example
<%= link_to 'Set as default', account_path(account, is_default: true), method: :put %>
Documentation: link_to
Try this
<%= link_to 'Set as default',{:controller => "" ,:action=>"" ,:id => ,:is_default=>true} %>
Related
Can i use variable for Rails link_to helper for making different link with variables?
For example,
<%= link_to users, users_path %>
I have link like this,
And i'd like to change this url with variable examples
So i changed url like this,
<%= link_to users, "#{examples}_path" %>
This not worked because of whole string change to url.
How can i change my link_to for use this with different variable for DRY codes?
What you're asking is really just how to perform dynamic method calls. In Ruby you can do it with send (and public_send):
<%= link_to users, send("#{examples}_path") %>
The difference between the two is that send will let you violate encapsulation and call private/protected methods.
You can also do it by calling call on the Method object:
<%= link_to users, method("#{examples}_path".to_sym).call %>
However you most likely don't even need this it in the first place. Just use the polymorphic routing helpers to create links from models:
# a link to show whatever resource happens to be
<%= link_to resource.name, resource %>
<%= link_to "Edit", edit_polymorphic_url(resource) %>
<%= link_to "New", new_polymorphic_url(resource_class) %>
<%= link_to "Frobnobize", polymorphic_url(resource, :frobnobize) %>
# a link to the index
<%= link_to resource_class.model_name.plural, resource_class %>
These all use a set of heuristics to figure out what the corresponing path helper is and then call it dynamically with send.
Or if you want to link to a specific controller or action just use the functionality provided by url_for:
# link to a specific controller action
<%= link_to "Baz", { controller: :foo, action: :bar } %>
# Will use the current controller
<%= link_to "Baz", { action: :bar } %>
I'm trying to link_to a url and would like to add the id at the end. The id is in a global variable #id:
<%= link_to "link", "https://www.example.com/sample-#{#id}" %>
The above returns this:
https://www.example.com/sample-#%3CItem:0x007fe66cf8e850%3E
Any idea how I can implement this?
how can I implement this?
Exactly like you did. Only make sure that #id contains an actual id (simple value) and not a complex object Item. Or rename it to #item.
If you're in a hurry, this is the lazy change:
<%= link_to "link", "https://www.example.com/sample-#{#id.id}" %>
i need to get a ajax tooltip on a dynamic link, so the logic seems to concatenate it. but, still not work, so, someone know a way to do this?
thank's
<%= link_to "Profile", edit_user_path(current_user), :class =>"ttooltip", :data => {:url => "/users/#{#current_user}/links"} %>
You're string interpolating the current user object, which will call .to_s on the user object, which probably isn't what you want.
If links is nested under each user, you typically follow the 'users/:id/links' so you need to interpolate the id instead of the user object like so:
<%= link_to "Profile", edit_user_path(current_user), :class =>"ttooltip", :data => {:url => "/users/#{current_user.id}/links"} %>
(Where current_user is a helper method that returns the current_user object.)
I used to have a link that lead to a search action and carried certain parameters. This was the link (for example):
<%= link_to "search", discover_search_url(:category_id => 3) %>
A friend of mine refactored the site and did a fantastic job of it, but now the category_id is inside a parent hash called pieces_search. So instead of using params[:category_id] now I use params[:pieces_search][:category_id].
My question is, how do I modify my link now?
<%= link_to "search", discover_search_url(:pieces_search => {:category_id => 3}) %>
This question already has answers here:
Add querystring parameters to link_to
(5 answers)
Closed 8 years ago.
I have a logic where I allow sorting on price and relevance. I am doing this by passing parameters to controller. My URL has a parameter - 'sort' which can have a value - 'price_lowest' or 'default'.
The links looks like:
lowest prices |
relevance
problem with the above code is that it "adds" parameters and does not "replace" them. I want to replace the value of &sort= parameter without adding a new value. E.g. I don't want :
../&sort=price_lowest&sort=price_lowest&sort=default
With the current logic - I am getting the above behaviour. Any suggestions ?
In order to preserve the params I did this:
<%= link_to 'Date', params.merge(sort: "end_date") %>
However the url will be ugly.
UPDATE
For Rails 5 use:
<%= link_to 'Date', request.params.merge(sort: "end_date") %>
If you only need one cgi param and want to stay on the same page, this is very simple to achieve:
<%= link_to "lowest prices", :sort => "price_lowest" %>
However, if you have more than one, you need some logic to keep old ones. It'd probably be best extracted to a helper, but essentially you could do something like this to keep the other params..
<%= link_to "lowest prices", :sort => "price_lowest", :other_param => params[:other] %>
Named routes will only really help you here if you need to go to another page.
If a path is not passed to the link_to method, the current params are assumed. In Rails 3.2, this is the most elegant method for adding or modifying parameters in a URL:
<%= link_to 'lowest prices', params.merge(sort: 'end_date') %>
<%= link_to 'relevance', params.merge(sort: 'default') %>
params is a Ruby hash. Using merge will either add a key or replace the value of a key. If you pass nil as the value of a key, it will remove that key/value pair from the hash.
<%= link_to 'relevance', params.merge(sort: nil) %>
Cite:
link_to http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
url_for
http://apidock.com/rails/v3.2.13/ActionView/Helpers/UrlHelper/url_for
My working solution on Rails 3.1
of course, it's hardcode, and has to be refactored.
item model
def self.get(field,value)
where(field=>value)
end
items controller
#items=Item.all
if params[:enabled]
#items=#items.get(:enabled, params[:enabled])
end
if params[:section]
#items=#items.get(:section_id, params[:section])
end
items helper
def filter_link(text, filters={}, html_options={})
trigger=0
params_to_keep = [:section, :enabled]
params_to_keep.each do |param|
if filters[param].to_s==params[param] && filters[param].to_s!="clear" || filters[param].to_s=="clear"&¶ms[param].nil?
trigger=1
end
if filters[param]=="clear"
filters.delete(param)
else
filters[param]=params[param] if filters[param].nil?
end
end
html_options[:class]= 'current' if trigger==1
link_to text, filters, html_options
end
items index.html.erb
<%= filter_link 'All sections',{:section=>"clear"} %>
<% #sections.each do |section| %>
<%= filter_link section.title, {:section => section} %>
<% end %>
<%= filter_link "All items", {:enabled=>"clear"} %>
<%= filter_link "In stock", :enabled=>true %>
<%= filter_link "Not in stock", :enabled=>false %>
It's not quite the answer to the question that you're asking, but have you thought about using the Sorted gem to handle your sorting logic and view links?