在不同的控制器内设计形式 [英] Devise form within a different controller

查看:92
本文介绍了在不同的控制器内设计形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我从devise生成了视图文件,使用 rails g devise views

code>



我看到有一个devise / sessions / new.html.erb文件,其中包含一个sign_in表单。



我创建了另一个文件devise / sessions / _form.html.erb,并在新的内容中执行<%= render'form'%> 。 html.erb文件,这样做很好。



现在,我想从不同的控制器中包含这个表单。所以在一个名为'main'的控制器中(具体来说,在view页面中)'mains / index.html.erb'我包含<%= render'devise / sessions / form'%> 文件。似乎包含工作正常,但我收到以下错误。

  Mains中的NameError index#b 
$ b显示/home/administrator/Ruby/site_v4_ruby/app/views/devise/sessions/_form.html.erb其中第1行提出:

未定义的局部变量或方法`resource'for#< ;#<班级:0x007f1aa042d530>:0x007f1aa042b870>
提取源(绕行#1):

1:<%= form_for(resource,:as => resource_name,:url => session_path(resource_name) F | %GT;
2:< p><%= f.label:email%>< br />
3:<%= f.text_field:email%>< / p>
4:

似乎form_for(资源,...)部分导致问题(如果我在原始设计sign_in页面上工作正常...我如何以rails方式解决这个问题?



我个人更喜欢使用'render'函数包含表单,而不是内联写入html代码。



我必须在'main'控制器内指定一些东西(资源)吗?



我将感谢您的帮助,谢谢。

解决方案

正如Andres所说,表单调用当您从非Devise控制器访问Devise表单时,Devise指定的帮助者不存在。



为了解决这个问题,您需要添加以下方法到控制器的帮助类,您希望显示窗体。或者,您可以将它们添加到应用程序帮助程序,使其可在任何地方使用。

  def resource_name 
:user
end

def resource
@resource || = User.new
end

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

资料来源: http://pupeno.com/blog/show-a-devise-log- in-form-in-another-page /


I am using a devise gem for sign_in/sign_out procedures.

I generated views files from devise, using rails g devise views

I saw there was a devise/sessions/new.html.erb file which contained a form for sign_in.

I created another file devise/sessions/_form.html.erb and did <%= render 'form' %> within a new.html.erb file, and that worked out very fine.

Now, I wanted to include this form from the different controller. So in a controller called 'main', (specifically, within view page) 'mains/index.html.erb' I included <%= render 'devise/sessions/form' %> file. It seems that inclusion worked fine, but I am getting the following error.

NameError in Mains#index

Showing /home/administrator/Ruby/site_v4_ruby/app/views/devise/sessions/_form.html.erb where line #1 raised:

undefined local variable or method `resource' for #<#<Class:0x007f1aa042d530>:0x007f1aa042b870>
Extracted source (around line #1):

1: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
2:   <p><%= f.label :email %><br />
3:   <%= f.text_field :email %></p>
4: 

It seems that form_for(resource,...) part is causing the problem (which works fine if I am on the original devise sign_in page... How can I resolve this problem in rails way?

I personally prefer to use 'render' function to include the form, rather than writing html codes inline.

Do I have to specify something (resource) within the 'main' controller?

I will appreciate your help. Thank you.

解决方案

As Andres says, the form calls helpers which are specified by Devise and so aren't present when you access a Devise form from a non-Devise controller.

To get around this, you need to add the following methods to the helper class of the controller you wish to display the form under. Alternatively, you can just add them to your application helper to make them available anywhere.

  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

Source: http://pupeno.com/blog/show-a-devise-log-in-form-in-another-page/

这篇关于在不同的控制器内设计形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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