Ruby on rails 如何使下拉菜单中的每个选项成为与关联简单表单调用的链接?
我使用simple_form插件创建了此表单:Ruby on rails 如何使下拉菜单中的每个选项成为与关联简单表单调用的链接?,ruby-on-rails,ruby,ruby-on-rails-3,hyperlink,simple-form,Ruby On Rails,Ruby,Ruby On Rails 3,Hyperlink,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 %
<%= 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 %>
试试
:collection=>@video.comment_titles.map{| ct |[ct,(链接到ct,comment_title_path(ct))}
我真的找到了答案。以下是ruby代码:
<%= f.association :comment_title, :collection => @video.comment_titles.map {|ct| [ct.title, comment_title_path(ct)] }, :label => "Comment Title:", :include_blank => false %>
很简单。哦,我甚至不知道这是可能的。。。我本来打算使用javascript,但这更干净。。。不过我会试着回答一个问题。。。为什么在链接之前的数组的第一个元素中有
ct
?那么,第一个元素被支持为选项值:链接到注释标题这是不正确的。我已经在问题详细信息中发布了根据您的代码生成的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 %>
$("select").change(function () {
var url = $("select option:selected").val();
$(location).attr("href",url);
});