2

我使用 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单独的显示视图的链接?

更新

这是从第一个答案的代码中生成的 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>
4

2 回答 2

3

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

<%= 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);
});

很简单。

于 2011-04-20T16:43:43.527 回答
1

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

于 2011-04-20T05:03:40.160 回答