Skip to content

Commit

Permalink
Add Pins Associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Gehani committed Sep 19, 2013
1 parent e8edabf commit 89454ce
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
26 changes: 23 additions & 3 deletions app/controllers/pins_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
class PinsController < ApplicationController
before_filter :authenticate_user!, except: [:index]
before_action :set_pin, only: [:show, :edit, :update, :destroy]

# GET /pins
# GET /pins.json
def index
@pins = Pin.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @pin }
end
end

# GET /pins/1
# GET /pins/1.json
def show
@pin = Pin.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @pin }
end
end

# GET /pins/new
def new
@pin = Pin.new
@pin = current_user.pins.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @pin }
end
end

# GET /pins/1/edit
def edit
@pin = current_user.pins.find(params[:id])
end

# POST /pins
# POST /pins.json
def create
@pin = Pin.new(pin_params)
@pin = current_user.pins.new(params[:pin])

respond_to do |format|
if @pin.save
Expand All @@ -40,8 +57,10 @@ def create
# PATCH/PUT /pins/1
# PATCH/PUT /pins/1.json
def update
@pin = current_user.pins.find(params[:id])

respond_to do |format|
if @pin.update(pin_params)
if @pin.update_attributes(params[:pin])
format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
format.json { head :no_content }
else
Expand All @@ -54,6 +73,7 @@ def update
# DELETE /pins/1
# DELETE /pins/1.json
def destroy
@pin = current_user.pins.find(params[:id])
@pin.destroy
respond_to do |format|
format.html { redirect_to pins_url }
Expand Down
6 changes: 5 additions & 1 deletion app/models/pin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Pin < ActiveRecord::Base
attr_accessible :description
attr_accessible :description

validates :description, presence: true

belongs_to :user

validates :user_id, presence: true
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>One Month Rails</title>
<title>Sample Application - Invite Me</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
Expand Down
17 changes: 9 additions & 8 deletions app/views/pins/_pin.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<% @pins.each do |pin| %>
<tr>
<td><%= pin.description %></td>
<td><%= link_to 'Show', pin %></td>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>

<tr>
<td><%= pin.description %></td>
<td><%= link_to 'Show', pin %></td>
<% if current_user == pin.user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
7 changes: 6 additions & 1 deletion app/views/pins/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
<p>
<%= @pin.description %>
</p>
<%= link_to 'Edit', edit_pin_path(@pin) %> |
<p>
<%= @pin.user.name %>
</p>
<% if current_user == @pin.user %>
<%= link_to 'Edit', edit_pin_path(@pin) %> |
<% end %>
<%= link_to 'Back', pins_path %>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130919011900) do
ActiveRecord::Schema.define(version: 20130919041835) do

create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end

add_index "pins", ["user_id"], name: "index_pins_on_user_id"

create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down

0 comments on commit 89454ce

Please sign in to comment.