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 %>

これにより、配列の最初の要素がテキストとして渡され、配列の2番目の要素が値として渡されます。次に、この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 に答える