Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overrided create action. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions spec/controllers/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,54 @@
end
end

describe '#create.js' do
let(:user) { double }

let(:params) { { 'email' => '[email protected]', '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' => '[email protected]', '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 }

Expand Down