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
Ruby on Rails - link_to button / css
提问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_to
generates 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_to
will 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 (a
element) with classes inbox-sf-add-btn
and tip
is generated, right? So, what's the problem?
但是它没有添加我的CSS样式
是什么意思?生成了a
带有类inbox-sf-add-btn
和的链接(元素)tip
,对吗?所以有什么问题?
If you want strictly button (input
element instead of a
), you can specify /book/new
(or <%= new_admin_course_path %>
) in the action
attribute of your html form (form
element).
如果你想要严格的按钮(input
元素而不是a
),你可以在你的 html 表单(元素)的属性中指定/book/new
(或)。<%= new_admin_course_path %>
action
form