Add css class to rails link_to helper

后端 未结 5 2005
星月不相逢
星月不相逢 2020-12-24 10:43

I\'m trying to style a rails link using css using the following code:

<%= link_to \"Learn More\", :controller => \"menus\", :action => \"index\", :         


        
相关标签:
5条回答
  • 2020-12-24 10:50

    Try new argument convention:

    <%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>
    
    0 讨论(0)
  • 2020-12-24 10:51

    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"
    
    0 讨论(0)
  • 2020-12-24 10:57

    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"
    # => <a href="/articles" class="article" id="news">Articles</a>
    

    Leaving the hash off gives the wrong link:

    link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
    # => <a href="/articles/index/news?class=article">WRONG!</a>
    

    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
    
    0 讨论(0)
  • 2020-12-24 11:08

    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'%>
    
    0 讨论(0)
  • 2020-12-24 11:10

    I solved my problem by the way

    <%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>
    
    0 讨论(0)
提交回复
热议问题