Ruby on Rails - link_to 按钮/css

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4418004/
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-08-29 23:18:28  来源:igfitidea点击:

Ruby on Rails - link_to button / css

ruby-on-railscss

提问by ChrisMJ

Ok so I am teaching myself RoR while developing a simple api & web interface. I have been following a number of guides and testing out different outcomes. I have mocked up some HTML templates and I am at the process where I am starting to strip them down into views.

好的,所以我在开发一个简单的 api 和 web 界面的同时自学了 RoR。我一直在遵循一些指南并测试不同的结果。我已经模拟了一些 HTML 模板,我正在开始将它们分解为视图。

In my HTML i have the following code, which is a Button with CSS styling:

在我的 HTML 中,我有以下代码,它是一个带有 CSS 样式的按钮:

 <input type="submit" name="" value="Add" id="open-contacts-dialog-btn" class="inbox-sf-add-btn tip" title="Open an dialog to add a new contact"/>  

This is my button in my panel that I would like to link to the /book/new/, I know i need a link_toin here but what would my final code be? The code I have tried is as follows

这是我面板中的按钮,我想链接到/book/new/,我知道这里需要一个link_to但我的最终代码是什么?我试过的代码如下

<%= link_to "Add", new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>

But it is not adding my CSS styling, it is just generating the text "Add". Any help would be useful. Thanks

但它没有添加我的 CSS 样式,它只是生成文本“添加”。任何帮助都会有用。谢谢

回答by PeterWong

link_togenerates a <a>tag, which is not input type="submit". What you should use should be a button_to, which generates a form with a input type="submit"button to the link:

link_to生成一个<a>标签,它不是input type="submit". 您应该使用的是 a button_to,它会生成一个带有input type="submit"链接按钮的表单:

<%= button_to "Add", new_admin_course_path, :id => "open-contacts-dialog-btn",
      :class => "inbox-sf-add-btn tip", :method => :get %>

Note the :method => :get. Without it, the button_towill generate a form with method set to post.

请注意:method => :get. 没有它,button_to将生成一个方法设置为 post 的表单。

回答by Robert

I recently had to do this because my form was acting differently for link_to than it was with button_to and I needed the functionality the link provided. The best solution I found does not use CSS at all but instead calls .html_safe to escape the html in the ruby code and properly display the link as a button. What you want to do is this:

我最近不得不这样做,因为我的表单对 link_to 的行为与对 button_to 的行为不同,我需要链接提供的功能。我发现的最佳解决方案根本不使用 CSS,而是调用 .html_safe 来转义 ruby​​ 代码中的 html 并将链接正确显示为按钮。你想要做的是:

<%= link_to "<button>Add</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>

回答by Nikita Rybak

But it is not adding my CSS styling
What do you mean? The link (aelement) with classes inbox-sf-add-btnand tipis generated, right? So, what's the problem?

但是它没有添加我的CSS样式
是什么意思?生成了a带有类inbox-sf-add-btn和的链接(元素)tip,对吗?所以有什么问题?

If you want strictly button (inputelement instead of a), you can specify /book/new(or <%= new_admin_course_path %>) in the actionattribute of your html form (formelement).

如果你想要严格的按钮(input元素而不是a),你可以在你的 html 表单(元素)的属性中指定/book/new(或)。<%= new_admin_course_path %>actionform