【问题标题】:create record based on collection_select dropdown根据 collection_select 下拉菜单创建记录
【发布时间】:2017-11-04 23:20:24
【问题描述】:

我正在尝试根据下拉选择创建播放列表记录。一旦功能正常,我将把它放在一个引导划分的下拉列表中。现在我有一个下拉列表和一个链接。该链接执行此操作,使用单击的视频中的值创建一个新的播放列表 obj,在添加为播放列表 obj 之前,一切都是 PORO,其中视频作为视频 obj 保存到数据库中。我想要做的是,如果您想将视频添加为播放列表 obj,您会看到相关播放列表名称的列表。当您单击名称时,它与link_to 执行相同的操作,但会使用名称参数来创建新记录。谢谢你的帮助。 PS。如果您有关于实施引导程序的提示也很有帮助:D

<%= form_tag :playlists, {action: 'create'} do %>
        <%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item'  %>
        <%= link_to 'Create Playlist', new_playlist_path(etag: video.etag, video_id: video.video_id, user_id: current_user.id,
                                                         img_high: video.img_high, img_default: video.img_default,
                                                         title: video.title, description: video.description), class: 'dropdown-item' %>
<% end %>

基本上,我想要做的是......能够做link_to 正在做的事情,但也可以带上播放列表名称

【问题讨论】:

    标签: ruby-on-rails activerecord bootstrap-4 erb collection-select


    【解决方案1】:

    绑定到 select 的 onchange 事件并触发它的表单提交。

    &lt;%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item', onchange: "this.form.submit()" %&gt;

    这是一个例子:

    <form action="https://google.com/">
      <select name="q" onchange="this.form.submit()">
        <option value="abc">Option 1</option>
        <option value="123">Option 2</option>
      </select>
    </form>
    

    【讨论】:

    • 天哪!我已经读过这方面的内容,但尽管我需要一些 JS 并且正在寻找没有那个的解决方案。至少对于这个功能。我现在要去试试这个。
    • 无法让它工作,也没有传递我正在寻找的信息:(
    【解决方案2】:

    终于能够通过参数,我仍然必须使用按钮,因为无法让onchange: 工作。

          <%= form_tag :playlists, {action: 'create'} do %>
              <%= hidden_field_tag :etag, video.etag %>
              <%= hidden_field_tag :video_id, video.video_id %>
              <%= hidden_field_tag :img_high, video.img_high%>
              <%= hidden_field_tag :img_default, video.img_default %>
              <%= hidden_field_tag :title, video.title %>
              <%= hidden_field_tag :published_at, video.published_at %>
              <%= hidden_field_tag :description, video.description %>
              <%= hidden_field_tag :user_id, current_user.id %>
    
            <%= collection_select :playlist, :name, current_user.playlists, :name, :name,  {class: 'dropdown-item', onsubmit: 'this.form.submit()'}  %>
            <%= submit_tag 'Add',  method: 'post' %>
    

    不知道我可以在那里使用hidden_field_tag:P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多