如何在下拉菜单中将每个选项与关联simple_form调用相关联? [英] How do you make each option in a drop down menu a link with the association simple_form call?

查看:119
本文介绍了如何在下拉菜单中将每个选项与关联simple_form调用相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用simple_form插件有这个表单:

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

我的问题是如何修改此代码,以便每个下拉项都是每个下拉项的链接 comment_title 的个人节目视图?

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?

更新

这是从第一个答案的代码生成的html:

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>


推荐答案

我实际上已经弄明白了。这是红宝石代码:

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

这会将数组中的第一个元素作为文本传递,将数组中的第二个元素传递给值。然后我使用这个jQuery代码:

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);
});

非常简单。

这篇关于如何在下拉菜单中将每个选项与关联simple_form调用相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆