Ruby-on-rails 链接到选项

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

link_to options

ruby-on-railstwitter-bootstrap

提问by Devan Beitel

So I'm trying to make this into a link_to link:

所以我试图把它变成一个 link_to 链接:

<a class="dropdown-toggle" data-toggle="dropdown" href="#">Sign-In <b class="caret"></b></a>

I have this so far, but can't figure out how to handle the <b class="caret"></b>. I have tried a do block at the end of the link_to, but only got an error.

到目前为止,我有这个,但无法弄清楚如何处理<b class="caret"></b>. 我在 的末尾尝试了一个 do 块link_to,但只有一个错误。

<%= link_to "Sign-In", new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} %>

I've just started learning Rails, and have searched around (including reading the RoR API docs) but have got nothing to work. Thanks for the help!

我刚刚开始学习 Rails,并四处搜索(包括阅读 RoR API 文档),但没有任何工作。谢谢您的帮助!

回答by My God

The link_tothat you provided is alright in terms of context.

link_to你提供的是在背景方面没有问题。

Let me tell you what it is upto:

让我告诉你它是什么:

This is the link that you mentioned:

这是您提到的链接:

<%= link_to "Sign-In", new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} %>

This is the corresponding html link formed by the above link_to:

这是上面link_to形成的对应html链接:

<a href="users/sessions/new" class="dropdown-toggle" data-toggle="dropdown">Sign-In</a>

Now consider this link:

现在考虑这个链接:

<%= link_to(raw("Sign-In" +("<b class= 'caret'></b>")), new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"}) %>

The above link will give you the following html link:

上面的链接将为您提供以下 html 链接:

<a class="dropdown-toggle" data-toggle="dropdown" href="users/sessions/new">Sign-In<b class= 'caret'></b></a>

Now you are almost close to your result. The only thing is the href.

现在你几乎接近你的结果了。唯一的就是href。

Ok let's modify the above link_toa bit with just a change in href pathas-

好的,让我们link_to稍微修改一下上面的内容,只需更改href 路径即可-

<%= link_to(raw("Sign-In" +("<b class= 'caret'></b>")), "#", :class => "dropdown-toggle", :data => {:toggle=>"dropdown"}) %>

The generated link is now equivalent to what you wanted:

生成的链接现在相当于你想要的:

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Sign-In<b class= 'caret'></b></a>

回答by darph

If you provide a block, you omit the first argument (normally the content of the a tag) and it will be replaced by what the block yields.

如果您提供一个块,则省略第一个参数(通常是 a 标签的内容),它将被块产生的内容替换。

<%= link_to new_user_session_path, :class => "dropdown-toggle", :data => {:toggle=>"dropdown"} do %>
  Sign-In <b class="caret"></b>
<% end %>

回答by ggcodes

put the link in a container it could be a div and put a class ="dropdown".

将链接放在一个容器中,它可以是一个 div 并放置一个类 =“dropdown”。

 <div class="dropdown">
    <%= link_to image_tag('icons/lock_icon.png') + " Log In" +('<b class="caret"></b>').html_safe , '#', { :class => 'dropdown-toggle', 'data-toggle' =>  'dropdown' } %>
 </div>

回答by Awais Shafqat

It must be a data attribute in link_to link_to remove_user_path(recipient_id: recipient.id) , method: :delete, data: {confirm: 'Are you sure you want to delete?'}

它必须是 link_to 中的数据属性 link_to remove_user_path(recipient_id: recipient.id) , method: :delete, data: {confirm: 'Are you sure you want to delete?'}