繁体   English   中英

如何使下拉菜单中的每个选项成为具有关联simple_form调用的链接?

[英]How do you make each option in a drop down menu a link with the association simple_form call?

我有使用simple_form插件的此表单:

<%= simple_form_for([@video, @video.comments.new], :remote => true) do |f| %>
  <%= f.association :comment_title, :collection => @video.comment_titles, :label => "Comment Title:", :include_blank => false %>
  <%= f.input :body, :label => false, :placeholder => "Post a comment." %>
  <%= f.button :submit, :value => "Post" %>
<% end %>

这将创建带有以下行的下拉列表:

<%= f.association :comment_title, :collection => @video.comment_titles, :label => "Comment Title:", :include_blank => false %>

我的问题是如何修改此代码,以便每个下拉项都链接到每个comment_title的单独显示视图?

UPDATE

这是从第一个答案的代码生成的html:

<select class="select optional" id="comment_comment_title_id" name="comment[comment_title_id]">
    <option value="&lt;a href=" comment_titles="" 224"="">#&lt;CommentTitle:0x10353b890&gt;"&gt;#&lt;CommentTitle:0x10353b890&gt;</option>
    <option value="&lt;a href=" comment_titles="" 225"="">#&lt;CommentTitle:0x1035296e0&gt;"&gt;#&lt;CommentTitle:0x1035296e0&gt;</option>
    <option value="&lt;a href=" comment_titles="" 226"="">#&lt;CommentTitle:0x1035295a0&gt;"&gt;#&lt;CommentTitle:0x1035295a0&gt;</option>    
</select>

我真的想通了。 这是红宝石代码:

<%= f.association :comment_title, :collection => @video.comment_titles.map {|ct| [ct.title, comment_title_path(ct)] }, :label => "Comment Title:", :include_blank => false %>

这会将数组中的第一个元素作为文本传递,并将数组中的第二个元素作为值传递。 然后,我使用以下jQuery代码:

$("select").change(function () {
      var url = $("select option:selected").val();
      $(location).attr("href",url);
});

很简单

尝试:collection => @video.comment_titles.map {|ct| [ct, (link_to ct, comment_title_path(ct))] } :collection => @video.comment_titles.map {|ct| [ct, (link_to ct, comment_title_path(ct))] }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM