2011-04-19 43 views
2

我必須使用simple_form插件這種形式:如何讓下拉菜單中的每個選項與關聯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> 

回答

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

很簡單。

1

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

+0

哦,不知道這甚至可能...我打算使用JavaScript,但這是更清潔...我會試試這 – 2011-04-20 05:04:51

+0

一個問題,雖然......爲什麼有'CT'在第一鏈接之前的數組元素? – 2011-04-20 05:06:19

+0

好吧,第一個元素是suppossed是一個選項值: 2011-04-20 05:21:39

相關問題