使用设计rails在单页中输入登录和注册。

时间:2022-11-20 07:36:03

I just created a new rails app and added devise to it. Having different page for each sign in (users/sign_in) and sign up (users/sign_up) page is given by devise itself. But i want to add both sign in and sign up form in single page of my custom controller (welcome/home).

我刚刚创建了一个新的rails应用,并添加了新设计。为每个登录(user /sign_in)和注册(users/sign_up)页面提供不同的页面,这是设计本身提供的。但是我想在我的自定义控制器(欢迎/home)的单页中添加登录和注册表单。

And also I am really confused on how users controller is mapped to pages in views/devise/..and want to bring sign in and sign out pages under one roof.

我也很困惑用户控制器是如何映射到视图/设计/。想在一个屋檐下签个字。

Can anyone let me know directions on how to modify code to achieve this.

有人能告诉我如何修改代码以实现这一点吗?

1 个解决方案

#1


5  

And also I am really confused on how users controller is mapped to pages in views/devise/

我也很困惑用户控制器是如何映射到视图/设计/中的页面的

It isn't.

它不是。

Devise uses its own controllers -

设计使用它自己的控制器-

  • sessions_controller.rb (for login / logout)
  • sessions_controller。rb(登入/登出)
  • registrations_controller.rb (for sign_up)
  • registrations_controller。rb(sign_up)
  • passwords_controller.rb (for forgot etc)
  • passwords_controller。rb(忘了等)
  • confirmations_controller.rb (for confirmations)
  • confirmations_controller。rb(确认)
  • unlocks_controller.rb (for invites, I believe)
  • unlocks_controller。rb(我相信邀请)

When you call devise_for in your routes, it creates a series of routes which maps to these controllers. Indeed, you can even override both the controllers and the paths themselves:

当您在路由中调用devise_for时,它将创建一系列映射到这些控制器的路由。实际上,您甚至可以覆盖控制器和路径本身:

devise_for :users, path: "auth", controllers: { sessions: "login" }, path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }

This means that all the devise routing paths are actually formed from the Devise gem itself, not the users controller.

这意味着所有的设计路由路径实际上都是由设计宝石本身形成的,而不是用户控制器。


Fix

修复

To fix your issue, you need to refer to the following - Include Custom Sign-In Form Anywhere In Your App

要解决您的问题,您需要参考以下内容——在应用程序的任何地方包括自定义登录表单

Specifically, you'll need to use the following:

具体来说,您需要使用以下内容:

#app/helpers/application_helper.rb
class ApplicationHelper
 def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

This will give you the ability to call the resource for Devise wherever you want, which means you'll be able to use the forms where you want in your application.

这将使您能够在任何需要的地方调用设计资源,这意味着您将能够在应用程序中使用所需的表单。

Now, what I would highly recommend is to keep your devise forms in the same place. Don't change them. You want to call them as partials

现在,我强烈建议你把你的设计表格放在同一个地方。不要改变它们。你想把它们叫做偏导数

Here's an example: http://firststopcosmeticshop.co.uk (login / register at top):

这里有一个例子:http://firststopcosmeticshop.co。英国(登入/注册):

使用设计rails在单页中输入登录和注册。

This is a development app we made a while back - it calls the login / register views as partials:

这是我们以前做过的一个开发应用程序——它调用登录/注册视图作为部分:

#app/views/layouts/application.html.erb
...
<%= render "devise/sessions/new" %>

How you style it will be dependent on your CSS.

它的样式取决于CSS。

If you'd like more information, please let me know!

如果您想了解更多信息,请告诉我!

#1


5  

And also I am really confused on how users controller is mapped to pages in views/devise/

我也很困惑用户控制器是如何映射到视图/设计/中的页面的

It isn't.

它不是。

Devise uses its own controllers -

设计使用它自己的控制器-

  • sessions_controller.rb (for login / logout)
  • sessions_controller。rb(登入/登出)
  • registrations_controller.rb (for sign_up)
  • registrations_controller。rb(sign_up)
  • passwords_controller.rb (for forgot etc)
  • passwords_controller。rb(忘了等)
  • confirmations_controller.rb (for confirmations)
  • confirmations_controller。rb(确认)
  • unlocks_controller.rb (for invites, I believe)
  • unlocks_controller。rb(我相信邀请)

When you call devise_for in your routes, it creates a series of routes which maps to these controllers. Indeed, you can even override both the controllers and the paths themselves:

当您在路由中调用devise_for时,它将创建一系列映射到这些控制器的路由。实际上,您甚至可以覆盖控制器和路径本身:

devise_for :users, path: "auth", controllers: { sessions: "login" }, path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }

This means that all the devise routing paths are actually formed from the Devise gem itself, not the users controller.

这意味着所有的设计路由路径实际上都是由设计宝石本身形成的,而不是用户控制器。


Fix

修复

To fix your issue, you need to refer to the following - Include Custom Sign-In Form Anywhere In Your App

要解决您的问题,您需要参考以下内容——在应用程序的任何地方包括自定义登录表单

Specifically, you'll need to use the following:

具体来说,您需要使用以下内容:

#app/helpers/application_helper.rb
class ApplicationHelper
 def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

This will give you the ability to call the resource for Devise wherever you want, which means you'll be able to use the forms where you want in your application.

这将使您能够在任何需要的地方调用设计资源,这意味着您将能够在应用程序中使用所需的表单。

Now, what I would highly recommend is to keep your devise forms in the same place. Don't change them. You want to call them as partials

现在,我强烈建议你把你的设计表格放在同一个地方。不要改变它们。你想把它们叫做偏导数

Here's an example: http://firststopcosmeticshop.co.uk (login / register at top):

这里有一个例子:http://firststopcosmeticshop.co。英国(登入/注册):

使用设计rails在单页中输入登录和注册。

This is a development app we made a while back - it calls the login / register views as partials:

这是我们以前做过的一个开发应用程序——它调用登录/注册视图作为部分:

#app/views/layouts/application.html.erb
...
<%= render "devise/sessions/new" %>

How you style it will be dependent on your CSS.

它的样式取决于CSS。

If you'd like more information, please let me know!

如果您想了解更多信息,请告诉我!