【问题标题】:Devise redirect and display error messages on fail with simple_form使用 simple_form 设计重定向并在失败时显示错误消息
【发布时间】:2015-03-13 11:04:56
【问题描述】:

我一直在审查使重定向正常工作的所有解决方案。我重写了注册控制器并且重定向工作,但是我的不足之处是获取简单的表单来处理错误。如果我通过标准路由到设计视图,它会显示正确的错误和 css,但是,当我在重定向到我的 root_path(pages#index) 路由时这样做时,我什么也看不到,并且表单没有改变.

表格代码:

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

    <%= f.error_notification %>

    <div class="form-group top">
      <%= f.input :name, required: true, placeholder: "George Washington" %>
    </div>
    <div class="form-group">
      <%= f.input :email, required: true, placeholder: "Your.email.address@email.com" %>
    </div>
    <div class="form-group">
      <%= f.input :password, required: true, hint: "7 characters minimum. No emojis allowed." %>
    </div>
    <div class="form-group">
      <%= f.input :password_confirmation, required: true %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Sign me up and get me my key", class: "btn btn-wide thirdy" %>
      <small class="note">By submitting your information you are agreeing to our Terms & Conditions.</small>
    </div>

  <% end %>

routes.rb:

Rails.application.routes.draw do
  devise_for :users, controllers: {
    registrations: "registrations"
  }

  get 'pages/index'
  get 'docs', to: 'pages#docs', as: 'docs'

  root to: 'pages#index'
end

registrations_controller.rb

def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      redirect_to root_path
    end
  end

pages_controller.rb

class PagesController < ApplicationController
  def index
  end

  def docs
  end
end

【问题讨论】:

  • 你能修复它吗?
  • 我做到了,再次感谢您的跟进。我遇到的问题是资源范围配置不正确。我将配置添加到 application_helper.rb,然后覆盖注册控制器的“创建”方法。出错时,我会“渲染”而不是“redirect_to”,它会使用我需要的错误消息破坏用户对象。我将编辑我的帖子以更好地说明我的解决方案。再次感谢您的帮助。

标签: ruby-on-rails ruby redirect devise ruby-on-rails-4.2


【解决方案1】:

当您重定向时,您实际上是在清理帖子正文 - 因此您没有任何可用于处理的信息。所以你不会看到任何错误 - 这是有道理的。

我通常做的是再次渲染部分/视图。举个例子:

def new
  @user = User.new
end

def create
  if @user.save
    redirect_to @user
  else
    # You have `params` with all the form data here
    # and also `@user.errors` with data from the validations
    render :new
  end
end

使用render :new,我将再次呈现表单,显示错误。

我相信你需要在那里做类似的事情才能显示错误。

但是,我不建议像这样覆盖创建操作 - 我会将 RegistrationsController 更新为如下内容:

class RegistrationsController < Devise::RegistrationsController
  private

  def after_sign_up_path_for(resource)
    root_path
  end
end

Devise 使用模板模式允许您在注册过程后更改目的地。

如果您想显示不同的 view 以防万一失败 - 就像您的 page/index,您必须在 registrations 模板文件夹中创建一个包含该内容的 create.html.erb 模板文件。

解决您的问题的方法

为了使它在注册失败的情况下呈现您的page/index,我会将该代码提取到部分代码,并提取到ApplicationController 中的帮助程序,以呈现该页面您需要做的所有事情。

所以,如果您有这样的index 操作(其中加载了一些内容):

def index
  @recent_news = News.recent_news
  @recent_users = User.recent_users
  @tags = Tag.ordered_by_number
end

我会将其内容提取为:

class ApplicationController < ActionController::Base
  def load_recent_content_and_tags # probably a better naming here
    @recent_news = News.recent_news
    @recent_users = User.recent_users
    @tags = Tag.ordered_by_number
  end
end

新的PagesController#index 会是这样的:

def index
  load_recent_content_and_tags
end

然后,在你的RegistrationsController

class RegistrationsController < Devise::RegistrationsController

  def create
    super do |resource|
      if resource.errors.present?
        load_recent_content_and_tags
        render 'pages/index' and return
      end
    end
  end 

  private

  def after_sign_up_path_for(resource)
    root_path
  end
end

我还没有测试过,但我相信它可能会如你所愿。

【讨论】:

  • 戴维斯,谢谢!我会试一试,让你知道。我也不喜欢我对控制器的实现。
  • 不,这只是说明常规流程如何工作的示例 - 当您提交表单时,该操作中的数据可用,如果您重定向,它将不再存在。你到底想要什么?您能否解释/详细说明用户旅程?
  • 是的。我有一个嵌入在索引/主页中的 sign_up 表单。当我提交并收到错误时,我想使用注册表单返回索引并显示错误。当我提交并强制错误时,它会重定向到设计视图。我确实创建了模板视图以匹配控制器,但它仍然重定向到设计默认值。如果需要更详细的说明,请告诉我。
  • 我会用你可能会尝试的东西来更新我的答案。
  • 我添加了一个新部分,A workaround for your problem,希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多