secret
Created

  • Download Gist
comments_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
def new
@comment = Comment.new
end
def create
@comment = Comment.new(comment_params)
 
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render action: 'show', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
 
private
 
def set_comment
@comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:author_id, :site_url, :content, :user_id, :song_id)
end
end
index.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<div id="layout1">
<h2>Songs</h2>
 
<ol>
<% @songs.each do |song| %>
<li><%= link_to song.title, song %><br></li>
Submitted <%= time_ago_in_words(song.created_at) + " ago" %>
<hr>
<%= link_to 'Show', song, class: "button small secondary" %>
<%= link_to 'Edit', edit_song_path(song), class: "button small secondary" %>
<%= link_to 'Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary"%>
 
 
<% end %>
</ol>
 
 
<br />
</div>
schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 
ActiveRecord::Schema.define(version: 20130713024850) do
 
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
 
create_table "comments", force: true do |t|
t.integer "song_id"
t.string "author_name"
t.string "site_url"
t.text "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
 
create_table "songs", force: true do |t|
t.string "title"
t.string "artist"
t.text "bio"
t.string "track_file_name"
t.string "track_content_type"
t.integer "track_file_size"
t.datetime "track_updated_at"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
 
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin"
end
 
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
 
end
songs_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
class SongsController < ApplicationController
before_action :set_song, only: [:show, :edit, :update, :destroy]
before_filter :authorize, only: [:create ,:edit, :update, :destroy]
 
# GET /Songs
# GET /Songs.json
def index
@songs = Song.all
end
 
# GET /Songs/1
# GET /Songs/1.json
def show
end
 
# GET /Songs/new
def new
@song = Song.new
end
 
# GET /Songs/1/edit
def edit
end
 
# POST /Songs
# POST /Songs.json
def create
@song = Song.new(song_params)
 
respond_to do |format|
if @song.save
format.html { redirect_to @song, notice: 'Song was successfully created.' }
format.json { render action: 'show', status: :created, location: @song }
else
format.html { render action: 'new' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
 
# PATCH/PUT /Songs/1
# PATCH/PUT /Songs/1.json
def update
respond_to do |format|
if @song.update(song_params)
format.html { redirect_to @song, notice: 'Song was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
 
# Song /Songs/1
# Song /Songs/1.json
def destroy
@song.destroy
respond_to do |format|
format.html { redirect_to songs_url }
format.json { head :no_content }
end
end
 
private
def set_song
@song = Song.find(params[:id])
end
def song_params
params.require(:song).permit(:title, :artist, :bio, :track)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.