我使用 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);
});
很简单。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)