[英]Add css class to rails link_to helper
我正在嘗試使用以下代碼使用css設置rails鏈接的樣式:
<%= link_to "Learn More", :controller => "menus", :action => "index", :class => "btn btn-inverse" %>
我希望這會創建一個如下所示的鏈接:
<a href="menus/" class="btn btn-inverse">Learn More</a>
相反,rails正在渲染這個 -
<a href="/menus?class=btn+btn-inverse">Learn More</a>
有沒有其他人有這個問題/知道我做錯了什么? 我知道我可以通過手動創建錨標記而不是使用幫助程序來避免這個問題,但我想知道是否有辦法將css類信息傳遞給幫助程序本身。 我正在使用Rails 3.2.6。
謝謝!
你有語法問題。 試試這個:
<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>
他們說:
使用舊參數樣式時要小心,因為需要額外的文字哈希:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article" # => <a href="/articles" class="article" id="news">Articles</a>
離開哈希會給出錯誤的鏈接:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article" # => <a href="/articles/index/news?class=article">WRONG!</a>
我建議您使用路由配置后生成的URL幫助程序 。 在你的情況下:
link_to "Learn More", menus_path, :class => "btn btn-inverse"
關於助手產生的一點提醒:
# 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
嘗試新的參數約定:
<%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>
我順便解決了我的問題
<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>
這就是我使用另一個視圖引擎HAML解決它的方法,以防其他開發人員滿足這種需求
%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class => "fa fa-plus-circle"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.