Rails 设计自定义注册和登录在同一页面问题 [英] Rails Devise Custom Registration and Login at the same page issues
问题描述
在我的 Ruby 应用程序中,我使用设计"gem 进行身份验证,但我想自定义视图,在自定义每个视图后生成设计视图并将它们作为部分保存在设计文件夹中,因为我想在另一个视图中呈现它们.
In my Ruby app, I am using the 'devise' gem for authentication but I want to customize the views, generate the devise views after customizing each of them and saving them in the devise folder as a partial, because I want to render them in another views.
所以我的问题是,new/sessions
正在运行,但是当我尝试注册时,表单发布似乎存在问题.请参阅此处的登录表单.
So my issues are, the new/sessions
is working, but when I try to sign up, it seems there's an issue with the form posting. See login form here.
我在设计文档中阅读了很多关于如何自定义布局的内容,但我对如何实现使表单呈现在设计视图文件夹之外工作感到有些困惑.
I read a lot about how to customize the layout in the devise documentation, but I am a little confused on how to implement to make the form render work outside the devise views folder.
推荐答案
其实你的问题和这个问题,但我会稍微解释一下,我会尝试去做.
Actually, your question is the same as this question, but I'll explain a little bit and I'm try to do it.
<小时>
循序渐进:
Step by step :
为自定义注册创建控制器并继承默认注册设备
# customize registration controller
class RegistrationsController < Devise::RegistrationsController
layout 'login'
skip_before_filter :require_no_authentication
before_filter :resource_name
def resource_name
:user
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
# another stuff here
end
end
为自定义会话创建控制器并继承默认会话设备
# customize session controller
class SessionsController < Devise::SessionsController
def create
super
end
end
创建注册视图 app/registrations/new.html.erb
将登录和注册表单放入 app/registrations/new.html.erb
.(在注册表旁边看起来像您的项目)
<%= form_for(resource, :as => resource_name, :url => user_session_path(resource_name)) do |f| %>
<%# another stuff here %>
<% end %>
<%= form_for(resource, :as => resource_name, :url => user_registration_path(resource_name)) do |f| %>
<%# another stuff here %>
<% end %>
覆盖应用助手中的设计映射方法
module ApplicationHelper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
自定义注册设备和会话的路由
devise_scope :user do
# using login path for registration
get '/login' => 'registrations#new', :as => :new_user_registration
post '/signup' => 'registrations#create', :as => :user_registration
post '/signin' => 'sessions#create', :as => :user_session
end
希望对您有所帮助!
这篇关于Rails 设计自定义注册和登录在同一页面问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!