Skip to content

Instantly share code, notes, and snippets.

@dzjin
Created April 18, 2014 20:49
Show Gist options
  • Save dzjin/11063781 to your computer and use it in GitHub Desktop.
Save dzjin/11063781 to your computer and use it in GitHub Desktop.
class FavoritesController < ApplicationController
before_filter :authenticate_user!
before_action :set_favorite, only: [:show, :edit, :update, :destroy]
# GET /favorites
# GET /favorites.json
def index
@favorites = current_user.favorites
end
# GET /favorites/1
# GET /favorites/1.json
def show
end
# GET /favorites/new
def new
@favorite = Favorite.new
end
# GET /favorites/1/edit
def edit
end
# POST /favorites
# POST /favorites.json
def create
@favorite = Favorite.new(favorite_params)
respond_to do |format|
if @favorite.save
format.html { redirect_to @favorite, notice: 'Favorite was successfully created.' }
format.json { render action: 'show', status: :created, location: @favorite }
format.js
else
format.html { render action: 'new' }
format.json { render json: @favorite.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /favorites/1
# PATCH/PUT /favorites/1.json
def update
respond_to do |format|
if @favorite.update(favorite_params)
format.html { redirect_to @favorite, notice: 'Favorite was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @favorite.errors, status: :unprocessable_entity }
end
end
end
# DELETE /favorites/1
# DELETE /favorites/1.json
def destroy
@favorite.destroy
respond_to do |format|
format.html { redirect_to favorites_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_favorite
@favorite = Favorite.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def favorite_params
params.require(:favorite).permit(:user_id, :video_id)
end
end
$("<%= escape_javascript(render @user) %>").appendTo("#users");
$("#<%= escape_javascript(@video_id) %>").attr('class', 'small button alert');
<h2>Videos</h2>
<ul class="large-block-grid-4 small-block-grid-2 medium-block-grid-3">
<% @videos.each do |video| %>
<li>
<div class="a-video">
<%= link_to video do %>
<div class="video-image text-center" style="background-image: url('<%= video.thumbnails[2].url%>')">
<p class="views"><%= video.views %> views</p>
<p class="duration"><%= video.time %></p>
</div>
<% end %>
<div class="title">
<p class="title-and-artist">
<%= link_to video.title, video, {:class=>"video-title"} %> </br >by
<%= link_to video.artist.name, video.artist, {:class=>"artist-name"} %>
<br />
<em>
tags:
<% if video.tag_list.size > 0 %>
<%= video.tag_list %>
<% else %>
none
<% end %>
</em>
<br />
<% if user_signed_in? %>
<%= link_to "Favorite", favorites_path(:user_id => current_user.id, :video_id => video.id), { :id=> video.id, :class => 'small button', remote: true } %>
<% else %>
<%= link_to "Favorite", favorites_path, { :id=> video.id, :class => 'small button' } %>
<% end %>
</p>
</div>
</div>
</li>
<% end %>
</ul>
<br>
<div class="paginates text-center">
<%= paginate @videos %>
<p><%= page_entries_info @videos %></p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment