Skip to content

Page 83 #22

@romenigld

Description

@romenigld

I'm at the page 83.
And so I add the store_location_for(:user, request.fullpath) in the accept method.
So when I run the rspec command I get another error:

rspec spec/features/accounts/accepting_invitations_spec.rb
.F

Failures:

  1) Accepting invitations accepts an invitation as an existing user
     Failure/Error: user = User.create!(user_params)

     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank

I have no Idea why is doing this now. I think it was the factory user because doesn't have the password_confirmation. But was not this the problem.

spec/features/accounts/accepting_invitations_spec.rb

require 'rails_helper'

feature "Accepting invitations" do
  let(:account) { FactoryGirl.create(:account) }
  let(:invitation) do
    Invitation.create(
      account: account,
      email: "[email protected]"
    )
  end

  before do
    InvitationMailer.invite(invitation).deliver_now
    set_subdomain(account.subdomain)
  end

  scenario "accepts an invitation" do
    email = open_email("[email protected]")
    accept_link = links_in_email(email).first
    expect(accept_link).to be_present
    visit accept_link
    fill_in "Password", with: "password"
    fill_in "Password confirmation", with: "password"
    click_button "Accept Invitation"
    expect(page).to have_content("You have joined the #{account.name} account.")
    expect(page.current_url).to eq(root_url(subdomain: account.subdomain))
  end

  scenario "accepts an invitation as an existing user" do
    email = open_email("[email protected]")
    accept_link = links_in_email(email).first
    expect(accept_link).to be_present

    visit accept_link
    click_link "Sign in as an existing user"
    user = FactoryGirl.create(:user)
    fill_in "Email", with: user.email
    fill_in "Password", with: "password"
    click_button "Log in"

    invitation_url = accept_invitation_url(invitation, subdomain: account.subdomain)

    expect(page.current_url).to eq(invitation_url)
    expect(page).to_not have_content("Sign in as existing user")
    click_button "Accept Invitation"
    expect(page).to have_content("You have joined the #{account.name} account.")
    expect(page.current_url).to eq(root_url)
  end
end

app/controllers/accounts/invitations_controller.rb

module Accounts
  class InvitationsController < Accounts::BaseController
    skip_before_action :authenticate_user!, only: [:accept, :accepted]
    before_action :authorize_owner!, except: [:accept, :accepted]

    def accept
      store_location_for(:user, request.fullpath)
      @invitation = Invitation.find_by!(token: params[:id])
    end

    def accepted
      @invitation = Invitation.find_by!(token: params[:id])
      user_params = params[:user].permit(
        :email,
        :password,
        :password_confirmation
      )

      user = User.create!(user_params)
      current_account.users << user
      sign_in(user)

      flash[:notice] = "You have joined the #{current_account.name} account."
      redirect_to root_url(subdomain: current_account.subdomain)
    end

    def new
      @invitation = Invitation.new
    end

    def create
      @invitation = current_account.invitations.new(invitation_params)
      @invitation.save
      InvitationMailer.invite(@invitation).deliver_later
      flash[:notice] = "#{@invitation.email} has been invited."
      redirect_to root_url
    end

    private

    def invitation_params
      params.require(:invitation).permit(:email)
    end

    def authorize_owner!
        unless owner?
          flash[:alert] = "Only an owner of an account can do that."
          redirect_to root_url(subdomain: current_account.subdomain)
        end
    end
  end
end

accept.html.erb

<div class="row">
  <div class="content col-md-offset-4 ">
    <h2>Accepting an invitation</h2>

    <!-- You are accepting an invitation to join the <%= @invitation.account.name %>. -->
    <p>
        You are accepting an invitation to join the <%= @invitation.account.name %> account.
    </p>
    <p>
      <%= link_to "Sign in as an existing user", new_user_session_path %>
    </p>

    <hr />

    <p>
      <h3>Sign up as a new user</h3>
    </p>
    <%= simple_form_for [:accepted, @invitation], as: :user do |f| %>
      <%= f.input :email, value: @invitation.email %>
      <%= f.input :password %>
      <%= f.input :password_confirmation %>
      <%= f.submit "Accept Invitation", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions