![](/img/trans.png)
[英]Selected drop down menu in simple_form when f.association is present in the view
[英]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="<a href=" comment_titles="" 224"="">#<CommentTitle:0x10353b890>">#<CommentTitle:0x10353b890></option>
<option value="<a href=" comment_titles="" 225"="">#<CommentTitle:0x1035296e0>">#<CommentTitle:0x1035296e0></option>
<option value="<a href=" comment_titles="" 226"="">#<CommentTitle:0x1035295a0>">#<CommentTitle:0x1035295a0></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.