Ruby-on-rails 将 css 类添加到 rails link_to helper

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13016295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:54:57  来源:igfitidea点击:

Add css class to rails link_to helper

ruby-on-rails

提问by Blake

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

我正在尝试使用以下代码使用 css 设置 rails 链接的样式:

<%= 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>

Instead, rails is rendering this -

相反,rails 正在呈现这个 -

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

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.

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

Thanks!

谢谢!

回答by MrYoshiji

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

一些文档可让您进一步使用 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>

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

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>


I recommend you to use the URL helpergenerated following your routes configuration. In your case:

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

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

A little reminder on the Helpers generated:

关于生成的 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

回答by Kamesh Prasad

Try new argument convention:

尝试新的参数约定:

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

回答by Caetano

I solved my problem by the way

我顺便解决了我的问题

<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>

回答by d1jhoni1b

This is how i solved it using another view engine, HAMLjust in case a fellow developer is having this need

这就是我使用另一个视图引擎HAML解决它的方法,以防其他开发人员有此需求

%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class  => "fa fa-plus-circle"

回答by SMAG

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

如果您没有链接所需的控制器操作/路由,您可以将 nil 作为占位符传递并根据需要应用类

<%= link_to 'link verbiage', nil,  class: 'classes for action tag'%>