我在主页上找到用户注册表时无法正常工作。它不断向我发送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
请帮忙
答案 0 :(得分:1)
更新'layout / index,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" }
答案 1 :(得分:0)
我猜你的问题是,如果你有错误(而不是static#home
),你想要呈现registrations#new
-
<强> form_for
强>
您遇到的问题是您的Rails应用重定向到model
控制器的registrations
路由(I.E registrations#new
)
当您使用form_for
(I.E创建一个新的ActiveRecord对象)时,Rails会重定向到它认为有必要呈现错误的表单(I.E registrations#new
):
通常,用于创建或更新资源的表单反映了 资源的身份有几种方式:(i)表单的URL 发送到(表单元素的action属性)应该导致a 请求被路由到适当的控制器操作(使用 适当:在现有资源的情况下为id参数)
-
<强>的Ajax 强>
我可以在这里谈论垃圾(我没有任何参考资料),但我会将注册表单包含为 ajax / remote
表单,然后发送相应的数据通过ajax,收到您可以在主页上显示的回复。
我们已经使用cosmetics app做了类似的事情(点击顶部的“注册”):
这可以通过将功能保留在registrations#new
控制器中,但允许您在主页上使用注册过程。
-
<强>代码强>
以下是我们使用的代码 - 请注意,您可以在“主页”中直接添加“在线”表单,您不需要像我们一样将其包含在popover中:
#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| %>