Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使下拉菜单中的每个选项成为与关联简单表单调用的链接?_Ruby On Rails_Ruby_Ruby On Rails 3_Hyperlink_Simple Form - Fatal编程技术网

Ruby on rails 如何使下拉菜单中的每个选项成为与关联简单表单调用的链接?

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插件创建了此表单:

<%= 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="&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>
<%= 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);
});