[英]RoR: Devise signup form on home page
当我在主页上保存用户注册表格时,我遇到了麻烦。 它一直将我发送到localhost:3000/signup
页面(出于验证目的,它会将我发送到那里,并显示错误)
该表格在我的layouts/index.html.erb
页面中
<%= 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 %>
这是我的路线:
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
这就是我的static#home
样子
def home
render :layout => "layouts/index"
end
请帮忙
更新“布局/索引,erb”文件
<%= 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 %>
并且不要尝试跳过,请覆盖您的控制器操作
devise_for :users, controllers: { sessions: "users/sessions" }
我猜你的问题是,如果您遇到错误(而不是registrations#new
),则想渲染static#home
-
form_for
您遇到的问题是您的Rails应用程序正在重定向到registrations
控制器的model
路由(IE registrations#new
)
当您使用form_for
(即IE创建一个新的ActiveRecord对象)时,Rails重定向到它认为对呈现具有错误的表单(IE registrations#new
)所必需的路由:
通常,旨在创建或更新资源的表单以多种方式反映资源的身份:(i)表单发送到的url(表单元素的action属性)应导致将请求路由到适当的控制器操作(对于现有资源,使用适当的:id参数)
-
阿贾克斯
我在这里可能是在谈论垃圾(我没有任何引用),但是我将注册表单作为ajax / remote
表单包括在内,然后通过ajax发送适当的数据,收到可以在您的显示器上显示的回报主页。
我们在化妆品应用程序中做了类似的操作(单击顶部的“注册”):
这可以通过将功能保留在registrations#new
控制器中而起作用,但是允许您在主页上使用注册过程。
-
码
这是我们使用的代码-请注意,您可以包括“在线”形式(直接在首页上),而无需像我们一样将其包括在弹出窗口中:
#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
这将允许您更改devise
registration
表格使用remote: :json
,具体如下:
#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| %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.