diff --git a/app/controllers/pins_controller.rb b/app/controllers/pins_controller.rb index 8404554..67068d5 100644 --- a/app/controllers/pins_controller.rb +++ b/app/controllers/pins_controller.rb @@ -1,5 +1,6 @@ class PinsController < ApplicationController before_action :set_pin, only: [:show, :edit, :update, :destroy] + before_filter :authenticate_user!, except: [:index] # GET /pins # GET /pins.json @@ -14,17 +15,18 @@ def show # GET /pins/new def new - @pin = Pin.new + @pin = current_user.pins.new 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 @@ -40,6 +42,7 @@ 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) format.html { redirect_to @pin, notice: 'Pin was successfully updated.' } @@ -54,6 +57,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 } diff --git a/app/models/pin.rb b/app/models/pin.rb index d246c90..81d211e 100644 --- a/app/models/pin.rb +++ b/app/models/pin.rb @@ -2,4 +2,7 @@ class Pin < ActiveRecord::Base attr_accessible :description validates :description, presence: true + + belongs_to :user + validates :user_id, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 5d12968..f5456db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,8 @@ class User < ActiveRecord::Base # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, #:recoverable, :rememberable, :trackable, :validatable - attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :provider, :uid, :as => [:default, :admin] + attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :first_name, :last_name, :provider, :uid, :as => [:default, :admin] + + has_many :pins, :dependent => :destroy end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 37287eb..846da14 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,8 +3,9 @@ <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, class: 'form-horizontal' }) do |f| %> <%= f.error_notification %> - <%= f.input :first_name %> - <%= f.input :last_name %> + <%#= f.input :first_name %> + <%#= f.input :last_name %> + <%= f.input :name %> <%= f.input :email %> <%= f.input :password, label: "New Password", autocomplete: "off" %> <%= f.input :password_confirmation, label: "New Password Confirmation", autocomplete: "off" %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 81aa9b5..1a18f8e 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,8 +3,10 @@ <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> <%= f.error_notification %> - <%= f.input :first_name %> - <%= f.input :last_name %> + <%#= f.input :first_name %> + <%#= f.input :last_name %> + <%# - how do I show the concatenation of first_name and last_name here %> + <%= f.input :name %> <%= f.input :email, placeholder: 'user@domain.com' %> <%= f.input :password, placeholder: 'hint: no special characters' %> <%= f.input :password_confirmation, placeholder: 're-type your password' %> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 7226b67..4fa4396 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -2,8 +2,6 @@ <%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> - <%= f.input :first_name %> - <%= f.input :last_name %> <%= f.input :email, placeholder: 'user@domain.com' %> <%= f.input :password, placeholder: 'hint: no special characters' %> <%= f.input :remember_me, as: :boolean %> diff --git a/app/views/pins/_pin.html.erb b/app/views/pins/_pin.html.erb index 6305c30..a3e9850 100644 --- a/app/views/pins/_pin.html.erb +++ b/app/views/pins/_pin.html.erb @@ -1,6 +1,8 @@
<%= @pin.description %>
- <%= link_to 'Edit', edit_pin_path(@pin) %> | - <%= link_to 'Back', pins_path %> ++ <%= @pin.user.name %> +
+ <% if current_user == @pin.user %> + <%= link_to 'Edit', edit_pin_path(@pin) %> | + <% end %> + <%= link_to 'Back', pins_path %> \ No newline at end of file diff --git a/db/migrate/20130917205250_add_user_id_to_pins.rb b/db/migrate/20130917205250_add_user_id_to_pins.rb new file mode 100644 index 0000000..1d4731f --- /dev/null +++ b/db/migrate/20130917205250_add_user_id_to_pins.rb @@ -0,0 +1,6 @@ +class AddUserIdToPins < ActiveRecord::Migration + def change + add_column :pins, :user_id, :integer + add_index :pins, :user_id + end +end diff --git a/db/migrate/20130918001115_add_name_to_user.rb b/db/migrate/20130918001115_add_name_to_user.rb new file mode 100644 index 0000000..74142b0 --- /dev/null +++ b/db/migrate/20130918001115_add_name_to_user.rb @@ -0,0 +1,5 @@ +class AddNameToUser < ActiveRecord::Migration + def change + add_column :users, :name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 69c6c86..73d22a2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,14 +11,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20130917193701) do +ActiveRecord::Schema.define(version: 20130918001115) 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 @@ -34,6 +37,7 @@ t.datetime "updated_at" t.string "first_name" t.string "last_name" + t.string "name" end add_index "users", ["email"], name: "index_users_on_email", unique: true