设计任何页面上的注册表格 [英] Devise Registration form on any page

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

问题描述

我想允许用户在我的首页/着陆页上注册该网站。
我已将设计注册表复制到我的着陆页视图中 -

 <%= form_for :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注册%>< / div>
<%end%>

<%= renderdevise / shared / links%>

我也有在我的 application_controller.rb c $ c>,以便我正确地定义资源 -

  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
然而,我仍然得到错误未定义的局部变量或方法

for#<#:0x007fa816b4b750>`



我在这里缺少什么才能在我的首页目标网页上正确呈现REGISTRATION表单?

解决方案

您需要声明这些控制器方法作为帮助方法来从视图访问它们:

  helper_method:resource,:resource_name,:devise_mapping 


$ b b

在定义方法后,只需在 ApplicationController 中添加该行。



你提供的链接实际上提到把这段代码放在应用程序 helper ,而不是控制器...你可以避免这个 helper_method 这样。


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" %>

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

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

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

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天全站免登陆