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

假装没事ソ 提交于 2019-12-24 04:23:27

问题


I have this form using the simple_form plugin:

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

and this creates a drop down list with this line:

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

My question is how do you modify this code so that each drop down item is a link to each comment_title's individual show view?

UPDATE

Here is the generated html from the code from the first answer:

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

回答1:


I actually figured it out. Here is the ruby code:

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

This passes the first element in the array as the text, and the second element in the array as the value. Then I use this jQuery code:

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

Pretty simple.




回答2:


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



来源:https://stackoverflow.com/questions/5720474/how-do-you-make-each-option-in-a-drop-down-menu-a-link-with-the-association-simp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!