I have an app in ruby on rails with a view which has some parameters displaying on screen.
<%= link_to l(:label_statistics),
{:action => 'stats', :id => #project, :repository_id => #repository.identifier_param},
:class => 'icon icon-stats' if #repository.supports_all_revisions? %>
I'd like to catch the variable #repository.name and use it in the function I have in my controller.
How do I to do it?
In your stats action, assuming you have a Repository model and the record containing name exists in your database.
#repository = Repository.find(params[:repository_id])
#repository.name
If not, you could pass it along with everything else,
<%= link_to l(:label_statistics),
{:action => 'stats',
:id => #project,
:repository_id => #repository.identifier_param,
:repository_name => #repository.name},
:class => 'icon icon-stats' if #repository.supports_all_revisions? %>
Then in the controller, access it via params[:repository_name]
Just pass it through the parameters. Doc for li_to.
link_to can also produce links with anchors or query strings:
link_to "Comment wall", profile_path(#profile, :anchor => "wall") # => Comment wall
link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails" # => Ruby on Rails search
link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux") # => Nonsense search
Related
I am trying to add a URL parameter in a link_to block.
The code currently <%= link_to "Submit", :action => 'renderChart', :class => "btn", :remote => true, :params => params.merge(:limit => 5) %>
but this gives me an error.
It adds the :class and :action into the url parameter, not just the :limit. Why?
EDIT:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
so when the user clicks the other link I want to add the limit to the url params and keep the sort params
Use this
<%= link_to "Submit",{ :action => 'renderChart', :remote => true, :limit => 5, :sort => "desc"}, :class => "btn" %>
Separate out the html_options: class is an html_option so pass it last.
Refer to link_to documentation.
UPDATE
As per the OP's concern in EDIT section of Question:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
params :sort => "desc" are for Toggle Sort Direction link and they cannot be connected to the Submit link. When you click on a particular link, params specified in the link would be added to the params hash. So, if you need to pass :sort => "desc" as params upon clicking on Submit link then specify them explicitly as shown in my answer above.
I finally managed to get a solution myself.
If I very simply do this: :params => {:limit => ..., :sort => params[:sort]} i get exactly what I need. If there is a sort param it keeps it the way it is.
You need to explicitly separate the hashes:
<%= link_to "Submit", { :action => 'renderChart', :class => "btn", :remote => true }, params.merge(:limit => 5) %>
Take the link_to out and you have an implicit hash (key-value pairs) and Ruby is smart enough to know you want a hash:
:action => 'renderChart', :class => "btn", :remote => true, params.merge(:limit => 5)
But that last thing - that's not a key-value pair - it's a hash. So really, you have this:
{ :action => 'renderChart', :class => "btn", :remote => true, { ... } }
If you take Rails out of the mix:
{ x: 'value', {} }
And that's simply not a valid Hash :)
am working in Rails version 2.3.8
Hash => agent_list = [[5, "val"], [4, "val"], [3, "val"], [1, "val"]]
<%= link_to_remote "Click Here",
:url => {
:controller => "controller",
:action => "method",
:id => #p_id,
:hash_list => hash_list
},
:method => 'post' %>
The link generated is :
[http://localhost/controller/method/12?hash_list%5B%5D%5B%5D=5&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=4&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=3&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=1&hash_list%5B%5D%5B%5D=val]
Could anyone tell me what is the right way to get something like:
http://localhost/controller/method/12?hash_list=[hash_list]
so that i can use it as params[:agent_list] in my controller method.
P.S. sorry if its a nooby question.
Create a route in your routes.rb to the action, if not already created, then:
<%= link_to "Click here", my_route_path(#obj, :hash => { :foo => "bar" }), :remote => true, :method => :post %>
untested but should do the trick. If you're supplying remote: true then it doesn't really matter what your URL looks like, the hash should reside in params[:hash] in your controller.
link_to_remote is deprecated in Rails 3+.
Rails 2.3.8:
<%= link_to_remote "Click here", :url => my_route_path(#obj, :hash => { :foo => "bar" }), :method => :post %>
My <%= link_to #item_link_name, show_item_path(item: {id: #item.id}) %>
generates http://localhost:3000/items/show_item?item%5Bid%5D=XXX,
what is recognised by controller as params[:item][:id]
I try to call an action of my Gallery controller from a Portfolio view. A Portfolio is made of many galleries.
I try this:
<%= link_to("Heart", gallery_path(gallery), :action => "like", :method => :put , :remote => true) %>
<%= link_to("Heart", :controller => :galleries, :action => "like", :method => :put , :remote => true) %>
And I obtain:
Heart
and
Heart
I want to get but i m stuck...:
Heart
Any RAILS God to help me ?
I believe you're getting behaviour because you're trying to use the "path helper" and "params hash" styles in the same link_to (see the docs for more details). I prefer the path helper style, so I'd write the link like this:
<%= link_to(
'Heart',
like_gallery_path(gallery),
{:method => :put, :remote => true}
) %>
If you like the params hash style, you'd write:
<%= link_to(
'Heart',
{:controller => 'galleries', :action => 'like', :id => gallery.id},
{:method => :put, :remote => true}
) %>
Note that the URL parameters (controller, action, etc.) are in a separate hash from the link parameters (method & remote).
Hope that helps!
Try with:
<%= link_to "Heart", gallery_path(gallery), :url => { :controller => "galleries", :action => "like"}, :method => :put, :remote => true) %>
I have a link_to helper that I'd love to use Twitter's Bootstrap.js to provide a popover. I have the following code:
"access", :action => "login" %>
I need to add the following HTML attributes: data-placement="" and rel="".
How do I do this using link_to? I've tried different variations of this:
<%= link_to "Login", 'data-placement' => 'below', :controller => "access", :action => "login" %>
<%= link_to "Login", html_options = {'data-placement' => 'below'}, :controller => "access", :action => "login" %>
<%= link_to "Login", :data => {'placement' => 'below'}, :controller => "access", :action => "login" %>
All these do is append things to my URL so it becomes:
http://localhost:3000/access/login?data-placement=below
I'm sure I've done this before!
-Jim
Apparently when using this format, I needed to specify the controller and action in a hash.
<%= link_to "Login", {:controller => "access", :action => "login"}, 'data-placement' => 'below' %>
I want to pass variable with link_to_remote in Rails 2.3. Following is my code for passing variable. But controller did not get that variable. Anybody can help me ?
<%= link_to_remote 'Add new event', :url => {:controller => 'events', :action => 'new' }, :with=> 'event' %><br>
Any hash key you add that's not part of the keyword set (controller, action, format) will be appended to your URL as an argument.
ie.
<%= link_to_remote "Add New Event", :url => {:controller => 'events', :action => 'new', :var => 'event'} %>
Would yield
/events/new?var=event
Hope this would solve your problem,I have not tried.
<%= link_to_remote "Add New event",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>