使用Rails / Devise的其他一组身份验证视图

时间:2013-02-27 20:01:33

标签: ruby-on-rails authentication devise

我有一个Ruby on Rails应用程序,它使用Devise(和Omniauth)进行身份验证。

我试图与想要从我的应用中嵌入网页的iOS应用程序(我无法控制)集成。这个应用程序需要我的页面具有特定的视觉外观,所以我想创建一组额外的身份验证视图。

在Devise文档中进行深入研究后,我收集到了可能需要在devise_scope中创建一个新的routes.rb块:

devise_scope :user do
    get    "iosapp/users/sign_in"  => "devise/sessions#iosapp_new"
    post   "iosapp/users/sign_in"  => "devise/sessions#iosapp_create"
    delete "iosapp/users/sign_out" => "devise/sessions#iosapp_destroy"
end

我创建了一组与这些路径相对应的新视图:

app/views/devise/sessions/iosapp_new.html.rb
app/views/devise/sessions/iosapp_create.html.rb
app/views/devise/sessions/iosapp_destroy.html.rb

但是在浏览器中加载 / iosapp / users / sign_in 会导致Rails错误:

undefined method `errors' for nil:NilClass

该错误源自devise_helper.rb(https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb)的第9行:

module DeviseHelper
  # A simple way to show error messages for the current devise resource. If you need
  # to customize this method, you can either overwrite it in your application helpers or
  # copy the views to your application.
  #
  # This method is intended to stay simple and it is unlikely that we are going to change
  # it to add more behavior or options.
  def devise_error_messages!
    return "" if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    ...

我显然在这里做错了什么,但是当我从" alternate"中调用时,无法弄清楚为什么resource未定义?观点。似乎我可能还需要创建其他控制器方法,但我无法在文档中找到有关此内容的任何内容。

我离开赛道了吗?或者有更好的方法来实现我的目标吗?

1 个答案:

答案 0 :(得分:1)

您的新观看次数需要form_for resource - 你有没有这样做?如果没有资源,它就无法在资源上找到错误,因此错误在nil。