Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

Help with a ruby on rails method

I'm trying to write a custom method in rails . the method is to add a video to a playlist .so far the method works fine . my issue is i want to be able to select the playlist tile , that i want to add the video instead having it define in the method.

def add_to_playlist
  @video = Video.find_by_slug(params[:id])
  @playlist =current_user.playlists.find_by_title("kompa")
  if @playlist.blank?
    respond_to do |format|
    format.html { redirect_to playlists_path, notice:'Create your first playlist to add videos' }
    end 
  else
    @video.playlists << @playlist
    @playlist.save 
    respond_to do |format|
      format.html { redirect_to browse_path,notice: 'Video was successfully added to the playlist.' }
    end
  end
end   

3 Answers

Alan Johnson
Alan Johnson
7,625 Points

You'll want to show something like a select box with the playlist options and then use the parameter to add it to the playlist. So, you'll need to add something like this to your form:

<%= f.collection_select :playlist_id, current_user.playlists, :id, :title, {include_blank: true} %>

and then in your controller you'll update the line where you set your playlist variable to be:

@playlist = current_user.playlists.find(params[:playlist_id)
Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

can you help me out , i fix my form but now when i try to add a video , i received an error " Couldn't find Playlist without an ID" here's my form

here's the view

<div class="videos">
    <div class="video_item">
      <%=link_to (image_tag (video.poster.url(:medium))), video_path(video)%>
        <div class="item_title">
          <%=link_to video.group, video_path(video)%>
        </div>
        <div class="item_sub">
          <%= link_to video.title, video_path(video) %>
        </div>

        <div class="clear"></div>

            <div class="clear"></div>
            <div class="thumbs_up">
               <%= form_for(Heart.new) do |f| %>
               <%= f.hidden_field :video_id, value: video.id %>
               <p><%=image_submit_tag("thumbsup_on.png" , :class => 'heart_hover') %></p>
               <% end %>
            </div>       
        <div class="play_item">
          <%= link_to ' ',video_path(video) , class: 'bigbtn'%>
        </div>

        <%if video.new? && video.created_at >= (Date.today)  %> 
        <div class="new_joint">
          <h3>new joint</h3>
        </div>
        <%end%> 
        <div id="myPlaylist" class="modal hide fade"  style="display:none;" >
              <%= render :partial => "/videos/select" %>
        </div>
        <div class="add_playlist " >
          <a href="#myPlaylist" role="button" data-toggle="modal"><img src="/assets/icon_plus_off.png" alt="add_btn"/></a>      
        </div>

    </div>

</div>

Since last night i'm trying to fix the errors .

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h1>Select Playlist</h1>
</div>
<%= form_for :playlist ,:url => {:action => :add_to_playlist} do |f| %>
<%if user_signed_in? %>
<div class="modal-body">
 <%= f.collection_select :playlist_id, current_user.playlists, :id, :title, {include_blank: true} %>
</div>
<%else%>
<div class="modal-body">
  <p class="generic">Please create playlist or register to create one<p>
</div>
<%end%>
<div class="modal-footer">
    <div class="form-actions">
    <%= f.submit "Add to playlist", class:'btn-profile'%>
    </div>
Alan Johnson
Alan Johnson
7,625 Points

Try updating this line. I'm pretty sure I messed you up :smile:

@playlist = current_user.playlists.where(id: params[:playlist_id).first