在任何页面设计注册表单 [英] Devise Registration form on any page

查看:21
本文介绍了在任何页面设计注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许用户在我的主页/登录页面上注册该网站.我已将设计注册表复制到我的登录页面视图中-

I'm trying to allow users to sign up for the site on my home/landing page. I've duplicated the devise registration form into my landing page view-

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></div>

  <div><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></div>

  <div><%= f.label :profile_name %><br />
  <%= f.text_field :profile_name %></div>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

我也在我的 application_controller.rb 中添加了辅助方法,以便我正确定义资源-

And I've also added the helper methods in my application_controller.rb so that I properly define the resource-

class ApplicationController < ActionController::Base
  protect_from_forgery
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

但是我仍然收到错误 undefined local variable or method resource'for #<#:0x007fa816b4b750>`

However I'm still getting the error undefined local variable or method resource'for #<#:0x007fa816b4b750>`

在我的主页登录页面上正确呈现注册表格时,我缺少什么?

What am I missing here to properly render a REGISTRATION form on my home landing page?

推荐答案

您需要将这些控制器方法声明为辅助方法,以便从视图中访问它们:

You need to declare those controller methods as helper methods to access them from the views:

helper_method :resource, :resource_name, :devise_mapping

只需在定义方法后在 ApplicationController 中添加该行即可.

Just add that line inside your ApplicationController after the methods are defined.

您会注意到您提供的链接实际上提到将此代码放在应用程序helper 中,而不是控制器中...如果你这样做.

You'll notice that the link you provided actually mentions to put this code in the application helper, not the controller... You can avoid this helper_method declaration if you do it that way.

这篇关于在任何页面设计注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆