ruby-on-railsactiverecordbootstrap-4erbcollection-select

create record based on collection_select dropdown


I am trying to create playlist records based on the dropdown selection. I'm going to put this in a bootstrap divided dropdown once functional. For now I have a dropdown and a link. The link does the thing, creates a new playlist obj with the values from the video it was clicked on, everything is a PORO until added as a playlist obj, where the video is persisted to the DB as a video obj. What I am wanting to do is, if you want to add a video to as a playlist obj, that you see a list of associated playlist names. When you click on the name, it does the same thing as the link_to but takes the name parameter along with it to create the new record. Thanks for any help. PS. if you have tips on implementing the bootstrap that'd be wicked helpful too :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 %>

Basically, what I'm trying to do is...be able to do what the link_to is doing but also take along the playlist name


Solution

  • Was finally able to get the passing the params, I still have to use a button as couldn't get the onchange: to work yet.

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

    Wasn't aware I could use the hidden_field_tag there like that :P