diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 190e007..f06c1a3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,4 @@ class ApplicationController < ActionController::Base - # Prevent CSRF attacks by raising an exception. - # For APIs, you may want to use :null_session instead. - # protect_from_forgery with: :exception - helper_method :collection, :resource, :current_user before_action :authenticate! diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 70f9c3b..04486da 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -3,6 +3,22 @@ class ProfilesController < ApplicationController before_action :build_resource, only: :create + def create + respond_to do |format| + format.html do + if resource.save + redirect_to profile_path(@user.id) + else + render :new + end + end + + format.js { render :errors unless resource.save } + + format.json { render :errors unless resource.save } + end + end + private def build_resource @user = User.new resource_params diff --git a/app/views/application/errors.js.erb b/app/views/application/errors.js.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/application/new.html.erb b/app/views/application/new.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/profiles/create.html.erb b/app/views/profiles/create.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/config/routes.rb b/config/routes.rb index c99e51f..1e4b830 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do resource :session, only: [:create, :destroy] - resource :profile, only: [:create, :show] + resources :profiles, only: [:create, :show] end diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index 485f1d6..2faba2c 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -25,6 +25,54 @@ end end + describe '#create.js' do + let(:user) { double } + + let(:params) { { 'email' => 'kathy@hartlova.com', 'password' => 'bigboobs', 'password_confirmation' => 'bigboobs' } } + + before { expect(User).to receive(:new).with(params).and_return(user) } + + context do + before { expect(user).to receive(:save).and_return(true) } + + before { post :create, user: params, format: :js } + + it { should render_template :create } + end + + context do + before { expect(user).to receive(:save).and_return(false) } + + before { post :create, user: params, format: :js } + + it { should render_template :errors } + end + end + + describe '#create.html' do + let(:user) { stub_model User, id: 1 } + + let(:params) { { 'email' => 'kathy@hartlova.com', 'password' => 'bigboobs', 'password_confirmation' => 'bigboobs' } } + + before { expect(User).to receive(:new).with(params).and_return(user) } + + context do + before { expect(user).to receive(:save).and_return(true) } + + before { post :create, user: params, format: :html } + + it { should redirect_to "/profiles/#{user.id}" } + end + + context do + before { expect(user).to receive(:save).and_return(false) } + + before { post :create, user: params, format: :html } + + it { should render_template :new } + end + end + context do let(:user) { double }