Skip to content

Commit 5af4c43

Browse files
committed
feature/Allow creation of user from sign up modal
1 parent 02e29bc commit 5af4c43

File tree

8 files changed

+85
-9
lines changed

8 files changed

+85
-9
lines changed

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
22
respond_to :html, :json
33
serialization_scope :view_context
44
before_action :configure_permitted_parameters, if: :devise_controller?
5+
before_action :set_toast_message, if: -> { params[:notice].present? || params[:alert].present? }
56

67
UNAUTHORIZED = 'You do not have access to this page'.freeze
78

@@ -12,4 +13,10 @@ def configure_permitted_parameters
1213

1314
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :bio, :location, :password, :current_password)}
1415
end
16+
17+
private
18+
19+
def set_toast_message
20+
flash[:notice] = params[:notice] || params[:alert]
21+
end
1522
end

app/controllers/home_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class HomeController < ApplicationController
22
def index
3+
@display_popover = true
4+
35
if user_signed_in?
46
@page_title = 'Feed'
5-
@display_popover = true
67
@snippets = current_user
78
.snippets_for_feed
89
.paginate(page: params[:page] || 1, per_page: 6)
@@ -11,7 +12,7 @@ def index
1112
@snippets = Snippet
1213
.includes(:user)
1314
.order(created_at: :desc)
14-
.paginate(page: params[:page] || 1)
15+
.paginate(page: params[:page] || 1, per_page: 6)
1516
end
1617

1718
respond_to do |format|
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
class RegistrationsController < Devise::RegistrationsController
2-
# def create
3-
# byebug
4-
# super
5-
# end
2+
def create
3+
respond_to do |format|
4+
format.html { super }
5+
format.json {
6+
user = User.new(user_params)
7+
8+
if user.save
9+
sign_in(user)
10+
render json: { message: 'Thanks for signing up, you are now logged in!' }
11+
else
12+
render json: { message: 'There was a problem creating your account.' }, status: 400
13+
end
14+
}
15+
end
16+
17+
end
618

719
def update
820
current_user.updated_at = Time.now
@@ -14,4 +26,10 @@ def update
1426
# render json: { message: "Failed to update profile" }, status: 400
1527
# end
1628
end
29+
30+
private
31+
32+
def user_params
33+
params.require(:user).permit(:email, :name, :password, :password_confirmation)
34+
end
1735
end

app/controllers/users/modals_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def sign_in
88
end
99

1010
def sign_up
11+
@user = User.new
12+
1113
render layout: false
1214
end
1315
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller } from 'stimulus';
2+
3+
export default class extends Controller {
4+
onSignUpSuccess(event) {
5+
const [data, status, xhr] = event.detail;
6+
const urlWithToast = encodeURI(`${location.toString()}?notice=${data.message}`)
7+
}
8+
9+
onSignUpError(event) {
10+
const [data, status, xhr] = event.detail;
11+
this.toast.display(data.message)
12+
}
13+
14+
get toast() {
15+
return document.getElementById('toast').toast
16+
}
17+
}

app/javascript/controllers/users_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class extends Controller {
1010
}
1111

1212
presentModal(event) {
13-
13+
event.stopPropagation()
1414
const target = event.currentTarget || event.target
1515
console.log('e', target.dataset)
1616
this.modal.present(target.dataset.modalUrl)
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1-
<div >
1+
<div data-controller="registrations">
22
<h2 class="text-center">Create an Account</h2>
3+
4+
<%= form_for@user, url: user_registration_path, remote: true, html: { data: { type: "html", action: "ajax:success->registrations#onSignUpSuccess ajax:error->registrations#onSignUpError" } } do |f| %>
5+
<%# <%= render "devise/shared/error_messages", resource: @user %>
6+
7+
<div class="field">
8+
<%= f.label :name %><br />
9+
<%= f.text_field :name, autofocus: true, autocomplete: "name" %>
10+
</div>
11+
12+
<div class="field">
13+
<%= f.label :email %><br />
14+
<%= f.email_field :email, autocomplete: "email" %>
15+
</div>
16+
17+
<div class="field">
18+
<%= f.label :password %>
19+
<% if @minimum_password_length %>
20+
<em>(<%= @minimum_password_length %> characters minimum)</em>
21+
<% end %><br />
22+
<%= f.password_field :password, autocomplete: "new-password" %>
23+
</div>
24+
25+
<div class="field">
26+
<%= f.label :password_confirmation %><br />
27+
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
28+
</div>
29+
30+
<div class="actions">
31+
<%= f.submit "Sign up" %>
32+
</div>
33+
<% end %>
334
</div>

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Rails.application.routes.default_url_options[:host] = 'cryptic-refuge-00081.herokuapp.com'
77
end
88

9-
devise_for :users
9+
devise_for :users, controllers: { registrations: 'registrations' }
1010
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
1111
root to: 'home#index'
1212

0 commit comments

Comments
 (0)