设计任何页面上的注册表格 [英] Devise Registration form on any page
问题描述
我想允许用户在我的首页/着陆页上注册该网站。
我已将设计注册表复制到我的着陆页视图中 -
<%= 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屋!