Add css class to rails link_to helper - ruby-on-rails

I'm trying to style a rails link using css using the following code:
<%= link_to "Learn More", :controller => "menus", :action => "index", :class => "btn btn-inverse" %>
I would expect that this would create a link that looks like this:
Learn More
Instead, rails is rendering this -
Learn More
Has anyone else had this problem / know what I'm doing wrong? I know I can avoid this problem by manually creating the anchor tag rather than using helper, but I was wondering if there was a way to pass the css class info to the helper itself. I'm using Rails 3.2.6.
Thanks!

You have a syntax problem. Try this instead:
<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>
Some documentation for you to go further with the link_to Helper
They say:
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
I recommend you to use the URL helper generated following your routes configuration. In your case:
link_to "Learn More", menus_path, :class => "btn btn-inverse"
A little reminder on the Helpers generated:
# routes.rb
resources :users
# any view/controller
users_path #=> /users
edit_user_path(user) #=> /users/:id/edit
user_path(user) #=> /users/:id (show action)
new_user_path(user) #=> /users/new

Try new argument convention:
<%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>

if you do not have a controller action / route necessary for the link, you can pass nil as the placeholder and get the classes to apply as necessary
<%= link_to 'link verbiage', nil, class: 'classes for action tag'%>

I solved my problem by the way
<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>

This is how i solved it using another view engine, HAML just in case a fellow developer is having this need
%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class => "fa fa-plus-circle"

Related

Rails no action "edit" in forum_posts_controller

The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

How to strip the index action part from link_to in Rails?

When I use:
link_to "Title", {:controller => 'my', :id=>1, :param=>2}
It produces:
<a href="/my/index?id=1&param=2">
But I want the following:
<a href="/my?id=1&param=2">
What is the best way to achieve this?
I use Rails 3.1.0
In general, it is best to use named routes, that you can later call as methods.
Example:
In routes.rb:
match '/my/:id/:param' => 'my#index', :as => :my
In a view:
link_to 'anchor', my_url(:id => 2, :param => 3)
Other than Achilles answer You can also give string parameter to link_to function
<%= link_to "Title", "/my/index?id=1&param=2" %>
You can also give string parameter to link_to function
<% id, param = 1,2 %>
<%= link_to "Title", "/my/index?id=#{id}&param=#{param}" %>

adding a class to a link_to is breaking the link

I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>

Rails: link_to with block and GET params?

How can I achieve query string and URL parameters in a link_to block declaration? Right now, I have this, which works:
<%= link_to 'Edit', :edit, :type => 'book', :id => book %>
The above works, and outputs:
http://localhost:3000/books/edit/1?type=book
What I want to do is something like this:
<% link_to :edit, :type => 'book', :id => book do %>
...
<% end %>
But the above format outputs:
http://localhost:3000/books/edit/
Which isn't what I'm looking for... I want it to output a URL like the previous example.
How can I achieve this?
link_to takes the same options that url_for does. Having said that, there is no :type option and it doesn't really accept blocks, so my guess is that the reason the your second example works is because it's located within the scope of a Book view. As mentioned by Tom in a reply to this answer, passing a block to link_to can be used as a replacement for the first argument (the link text).
If Book is a resource, you can get the link_to helper to generate the URL you're looking for by passing it one of the handy, automatically generated resource routes rails makes for you. Run rake routes before you try this:
<%= link_to "Edit", edit_book_path(book) %>
Otherwise, you can explicitly state what controller/action you want to link to:
<%= link_to "Edit", :controller => "books", :action => "edit", :id => book %>
Happy hacking.
EDIT: Almost forgot, you CAN add query strings bypassing them in AFTER you declare the id of the object you're linking to.
<%= link_to "Edit", edit_book_path(book, :query1 => "value", :query2 => "value")
Would product /books/1/edit?query1=value&query2=value. Alternatively:
<%= link_to "Edit", :controller => "books", :action => "edit", :id => book, :query1 => "value", :query2 => "value" %>
Try Follwing
<% link_to(:edit, :type => 'book', :id => book) do %>
...
<% end %>
or to achieve same url Use
<% link_to(:action=>'edit', :type => 'book', :id => book) do %>
...
<% end %>
Ruby doesn't know if you're sending the do ... end block to link_to or book, and is sending it to book because it is closer to the block. book do ... end returns nil, so you're left with link_to :edit, :type=>'book', :id=>nil. You will need to bracket the parameters, and while you're at it, I would rewrite it to be more understandable with a controller, action, id setup: link_to{:controller=>"books",:action=>"edit",:id=>book}do ... end
in mime_types.rb file add:
Mime::Type.register "text/application", :book

link_to send parameters along with the url and grab them on target page

How can i have a link on a page that takes the user to another URL and passes along a parameter and on the target url how can we pick up that parameter.
usually I add links like following:
<%= link_to "Add Product", '/pages/product' %>
But how can I send parameters along with this url? Can I pick them in the target action by using params[:parm_name]
Just add them to link:
<%= link_to "Add Product", '/pages/product?param1=value1&param2=value2' %>
and in controller:
param1 = params[:param1] # "value1"
param2 = params[:param2] # "value2"
If you use helper methods for routes (for example company_path), then you can add hash of params, so this two should be similar:
<%= link_to "Add Product", new_product_path(:param1 => "value1", :param2 => "value2") %>
<%= link_to "Add Product", "/products/new?param1=value1&param2=value2" %>
From documentation:
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
Here's a more rails-y way of doing it.
<%= link_to 'Link Text',
{controller: 'controller/name', action: 'action_name', query: params[:query]},
method: 'get',
:class=>'link_styling' %>
You need to reference your params in the hash defining the link. It also needs to be a GET method. Styling is optional of course.
This should really be here too: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Resources