【问题标题】:Devise Error Messages and Routings Rails 4设计错误消息和路由 Rails 4
【发布时间】:2014-05-14 07:35:56
【问题描述】:

我有一个设计附带的编辑页面。要在我的应用程序中使用它,我需要呈现一个特殊的布局,并且该布局应该只在编辑页面中呈现。 所以为了覆盖我写了编辑方法.. 这是我的registration_controller.rb

def edit
    session[:tab] = "Edit Profile"
    render layout: 'calculator'
  end

  def update

    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
    p '----------------'

    resource_updated = update_resource(resource, account_update_params)
    yield resource if block_given?
    if resource_updated
      if is_flashing_format?
        flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
          :update_needs_confirmation : :updated
        set_flash_message :notice, flash_key
      end
      sign_in resource_name, resource, bypass: true
      respond_with resource, location: after_update_path_for(resource)
    else
      clean_up_passwords resource
      redirect to '/users/edit'
    end
  end

如果我输入了错误的密码,就会发生错误并且渲染失败。 所以我想在我的更新功能中重定向到编辑网址。事情是当它重定向到编辑页面时我也想显示错误消息。

或者有什么简单的方法可以做到这一点。我想在编辑页面中呈现一个布局,当更新时发生错误时不会中断(如密码不匹配)

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 devise


【解决方案1】:

Flash

阅读了有关redirecting with flash 的这个问题后,似乎使用redirect_to 并不能维护您在控制器中设置的flash

要解决此问题,您可以使用flash.keep

def update
  ...
  else
     flash.keep #-> added
     ...
     redirect to '/users/edit'
  end
end

系统

在我看来,您的系统效率非常低 - 我知道您复制了设计使用的文件,但它仍然臃肿。

您还在控制器中使用puts 吗?不!你应该只在你的views中输出数据!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2022-11-26
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多