在表单标签中包含链接?

时间:2012-06-17 22:18:49

标签: ruby-on-rails ruby-on-rails-3

我希望在表单标签中包含一个链接:

<%= form.check_box 'eula' %>
<%= form.label 'eula', "I agree to the <a href='#' id='eula-link'>EULA</a>", class: 'label-checkbox' %>

Rails将HTML写出来,可能应该如此,但我将如何实现呢?单击EULA会打开一个JS弹出窗口。我想在某种程度上嵌入一个link_to?

7 个答案:

答案 0 :(得分:20)

html_safe与parens一起使用将呈现html,如下所示:

<%= f.input :eula, :as => :boolean, label: ("I agree to the #{link_to 'Terms of Service', terms_path}.").html_safe %>

答案 1 :(得分:2)

假设你正在使用vanilla rails形成助手,你可以这样做:

f.label :eula do
    'I agree to the #{link_to("EULA", "#")}'
end

来源:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label

答案 2 :(得分:1)

尝试了19种方法,其中超链接被编码或者html_safe替换了url中的连字符???

这对我有用

<%= f.label :cookies do
      "Do you agree to our #{link_to('Cookies Policy', 'http://www.your-­url.co.uk/privacypolicy')}".html_safe
end %>

“和”的具体用法显得很重要。

答案 3 :(得分:1)

jenson-button-event 的答案几乎对我有用,但是要求更改括号的位置接近加载而没有错误。

对我来说,下面解决了它。请注意Cookie政策&#39;之后的密切括号。在这里,而不是在链接路径本身之后。

<%= f.label :cookies do
      "Do you agree to our #{link_to('Cookies Policy'), 'http://www.your-­ url.co.uk/privacypolicy'}".html_safe
end %>

答案 4 :(得分:0)

尝试“我同意#{link_to'EULA',#,:id =&gt;'eula-link'}”

答案 5 :(得分:0)

我想要一种简单的方法来在表单字段标签后添加真棒的帮助链接按钮,这是我在haml文件中使用的方法:

    = form_for [@preplan, @structure] do |f|
      = f.label :template do
        Template
        = link_to 'https://intercom.help/blazemark/preplans-and-structures/structure-templates', target: '_blank' do
          = fa_icon 'fw info-circle'

enter image description here

答案 6 :(得分:0)

从Rails 6.0.2.1(到2020年1月)开始,这对我有用:

<div class="form-group form-check">
  <%= form.check_box :accept_terms, class: "form-check-input", required: true %>
  <%= form.label :accept_terms, class: "form-check-label" do %>
    <span>
      Accept <%= link_to 'Terms and Conditions', 'https://your.url.here.com' %>
    </span>
  <% end %>
</div>
相关问题