"index", :class => "btn btn-inverse" %> 我希望这会-6ren">
gpt4 book ai didi

ruby-on-rails - 将 css 类添加到 Rails link_to helper

转载 作者:行者123 更新时间:2023-12-03 05:51:39 25 4
gpt4 key购买 nike

我正在尝试使用以下代码使用 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>

还有其他人遇到过这个问题/知道我做错了什么吗?我知道我可以通过手动创建 anchor 标记而不是使用助手来避免这个问题,但我想知道是否有一种方法可以将 css 类信息传递给助手本身。我正在使用 Rails 3.2.6。

谢谢!

最佳答案

您有语法问题。试试这个:

<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>

Some documentation for you to go further with the link_to Helper

他们说:

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>
<小时/>

我建议您使用根据路由配置生成的 URL 帮助程序。对于您的情况:

link_to "Learn More", menus_path, :class => "btn btn-inverse"

关于生成的 Helpers 的一点提醒:

# 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

关于ruby-on-rails - 将 css 类添加到 Rails link_to helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13016295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com