根据collection_select下拉列表创建记录

时间:2017-11-04 23:20:24

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

我正在尝试根据下拉列表选择创建播放列表记录。一旦功能正常,我将把它放在一个bootstrap分割下拉列表中。现在我有一个下拉列表和一个链接。该链接执行该操作,使用点击的视频中的值创建新的播放列表obj,所有内容都是PORO,直到添加为播放列表obj,其中视频作为视频obj持久保存到DB。我想要做的是,如果你想将视频作为播放列表obj添加,你会看到相关播放列表名称的列表。当您单击名称时,它与link_to执行相同的操作,但会使用name参数来创建新记录。谢谢你的帮助。 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正在做的事情,但也会接受播放列表名称

2 个答案:

答案 0 :(得分:0)

绑定到选择的onchange事件并触发其提交表单。

<%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item', onchange: "this.form.submit()" %>

以下是一个例子:

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

答案 1 :(得分:0)

终于能够通过params,我仍然必须使用一个按钮,因为无法让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