将 css 类添加到 rails link_to helper [英] Add css class to rails link_to helper

查看:35
本文介绍了将 css 类添加到 rails link_to helper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<a href="menus/" class="btn btn-inverse">Learn More</a>

相反,rails 正在渲染这个 -

Instead, rails is rendering this -

<a href="/menus?class=btn+btn-inverse">Learn More</a>

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

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.

谢谢!

推荐答案

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

You have a syntax problem. Try this instead:

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

一些文档,可让您进一步了解 link_to帮手

他们说:

使用旧的参数样式时要小心,因为需要额外的文字散列:

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"

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

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

这篇关于将 css 类添加到 rails link_to helper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆