我有以下 HTML 和 .erb 代码:
<button type="button" class="btn btn-default btn-lg"><%= link_to "Customer Information", customer_information_path %></button>
该链接呈现良好,但在单击时什么也不做。
这是我的路线文件:
devise_scope :user do
root :to => "devise/sessions#new"
end
get "hub" => "pages#hub"
get "customer_information" => "pages#customer_information"
这是我的页面 Controller :
class PagesController < ApplicationController
def hub
end
def customer_information
end
end
代码不会抛出任何错误。
想法?
最佳答案
link_to
助手生成一个 <a>
元素,你不能放 <a>
在 <button>
里面, 那会违反 the content model for <button>
:
Content model:
Phrasing content, but there must be no interactive content descendant.
和interactive content包括 <a>
.如果你给浏览器输入一个 <button><a></a></button>
,那么它可能会重写 HTML 使其有效,或者它可能不会让点击到达 <a>
.
考虑使用 button_to
生产 <button>
而不是 <button>
和 link_to
或尝试向 <button>
添加点击处理程序在 JavaScript 中。使用 button_to
尽管 button_to
可能会使您的 CSS 复杂化将添加很多额外的标记(例如 <form>
和 <div>
s)。另一种选择可能是仅使用 <a>
工具栏中的元素,但将它们设置为看起来像按钮,那么你可以只使用 link_to
对于整个工具栏。
关于ruby-on-rails - Rails link_to 在按钮元素中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24847018/