Reputation: 8587
I'm having trouble getting user registration form to work when I have it in my home page. It keeps sending me to localhost:3000/signup
page (it sends me there because of validation purposes and shows the errors)
This form is in my layouts/index.html.erb
page
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.text_field :username %>
<%= f.email_field :email %>
<%= f.password_field :password %>
<%= button_tag :type => :submit do %>
Sign up
<% end %>
<% end %>
This is what my routes look like:
devise_for :users,
:skip => [:sessions],
:controllers => { registrations: "users/registrations", omniauth_callbacks: "omniauth_callbacks" }
devise_scope :user do
get 'signup/' => 'devise/registrations#new', :as => :new_user_registration
post 'signup/' => 'users/registrations#create', :as => :user_registration
end
end
authenticated :user do
root :to => "dashboard#show"
end
unauthenticated :user do
devise_scope :user do
root :to => "static#home"
end
end
This is what my static#home
looks like
def home
render :layout => "layouts/index"
end
Please help
Upvotes: 1
Views: 1772
Reputation: 76784
I'm guessing your problem is you want to render static#home
if you've got an error (instead of registrations#new
)
--
form_for
The problem you have is your Rails app is redirecting to the model
route for your registrations
controller (I.E registrations#new
)
When you use form_for
(I.E create a new ActiveRecord object), Rails redirects to the route it deems is necessary to render the form with errors (I.E registrations#new
):
Typically, a form designed to create or update a resource reflects the identity of the resource in several ways: (i) the url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource)
--
Ajax
I could be talking rubbish here (I don't have any references), but what I would include the sign up form as an ajax / remote
form, and then send the appropriate data via ajax, receiving a return which you can display on your homepage.
We've done something similar with our cosmetics app (click "register" at top):
This works by keeping the functionality in the registrations#new
contorller, but allows you to use the registration process on the homepage.
--
Code
Here is the code we used - please note that you can include the form "in-line" (on the homepage directly), you don't need to include it in a popover like we did:
#app/controllers/registrations_controller.rb
class RegistrationsController < DeviseController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
before_filter :configure_permitted_parameters
prepend_view_path 'app/views/devise'
# GET /resource/sign_up
def new
build_resource({})
respond_with self.resource
end
# POST /resource
def create
build_resource(sign_up_params)
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_to do |format|
format.json { render :json => resource.errors, :status => :unprocessable_entity }
format.html { respond_with resource }
end
end
end
# GET /resource/edit
def edit
render :edit
end
# PUT /resource
# We need to use a copy of the resource because we don't want to change
# the current user in place.
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
if update_resource(resource, account_update_params)
if is_navigational_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
sign_in resource_name, resource, :bypass => true
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
end
# DELETE /resource
def destroy
resource.destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :destroyed if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
def cancel
expire_session_data_after_sign_in!
redirect_to new_registration_path(resource_name)
end
protected
# Custom Fields
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name,
:email, :password, :password_confirmation)
end
end
def update_needs_confirmation?(resource, previous)
resource.respond_to?(:pending_reconfirmation?) &&
resource.pending_reconfirmation? &&
previous != resource.unconfirmed_email
end
# By default we want to require a password checks on update.
# You can overwrite this method in your own RegistrationsController.
def update_resource(resource, params)
resource.update_with_password(params)
end
# Build a devise resource passing in the session. Useful to move
# temporary session data to the newly created user.
def build_resource(hash=nil)
self.resource = resource_class.new_with_session(hash || {}, session)
end
# Signs in a user on sign up. You can overwrite this method in your own
# RegistrationsController.
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
# The path used after sign up. You need to overwrite this method
# in your own RegistrationsController.
def after_sign_up_path_for(resource)
after_sign_in_path_for(resource)
end
# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource)
respond_to?(:root_path) ? root_path : "/"
end
# The default url to be used after updating a resource. You need to overwrite
# this method in your own RegistrationsController.
def after_update_path_for(resource)
signed_in_root_path(resource)
end
# Authenticates the current scope and gets the current resource from the session.
def authenticate_scope!
send(:"authenticate_#{resource_name}!", :force => true)
self.resource = send(:"current_#{resource_name}")
end
def sign_up_params
devise_parameter_sanitizer.sanitize(:sign_up)
end
def account_update_params
devise_parameter_sanitizer.sanitize(:account_update)
end
end
This will allow you to change the devise
registration
form to use remote: :json
, as follows:
#app/views/devise/registrations/new.html.erb
<%= form_for(devise_resource, :as => devise_resource_name, :html => {:id => 'register_form'}, :url => user_registration_path, :remote => :true, :format => :json) do |f| %>
Upvotes: 0
Reputation: 490
Update the 'layout/index,erb' file
<%= form_for(User.new, :url => new_user_registration_path) do |f| %>
<%= f.text_field :username %>
<%= f.email_field :email %>
<%= f.password_field :password %>
<%= button_tag :type => :submit do %>
Sign up
<% end %>
<% end %>
and don't try to skip, Please override your controller action
devise_for :users, controllers: { sessions: "users/sessions" }
Upvotes: 1