【问题标题】:Redirecting when editing/updating a user (with error)编辑/更新用户时重定向(有错误)
【发布时间】:2011-05-06 08:43:36
【问题描述】:

我想在我的项目中从我自己的表单更新/编辑设计用户,但问题是我无法重定向“request.referer”的更新。

我读过,但它对我不起作用:https://github.com/plataformatec/devise/wiki/How-To:-Customize-the-redirect-after-a-user-edits-their-profile ...以及其他 wiki 页面。

好的,所以我的代码是:

#/views/backend/perso.html.erb
<%= form_for(@user, :url => registration_path(@user), :html => { :method => :put }) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(form.errors.count, "error") %> prohibited this data from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
# Other fields ...
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

所以用户在该页面上,并更新他的数据。问题,我被重定向到 /users/ 我尝试将其添加到路线中:

#config/routes.rb
devise_for :users do
    get "users", :to => "backend#perso", :as => :user_root # Rails 3
end

甚至到应用程序控制器:

#/controllers/application_controller.rb
private
def after_update_path_for(resource)
  backend_perso_path
end

但还是不行。

感谢任何试图帮助我的人!

编辑

当更新没有产生错误时,我会被重定向到我想要的页面(通过在我的应用程序控制器中添加 after_update_path_for),但是当存在错误时,它会显示 /view/devise/registration/edit.html.erb

更新

好的,所以我在下面覆盖了 Devise 控制器: https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password

所以我在registrations_controller 中的代码是这样的

#controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def update
    # Devise use update_with_password instead of update_attributes.
    # This is the only change we make.
    if resource.update_attributes(params[resource_name])
      set_flash_message :notice, :updated
      # Line below required if using Devise >= 1.2.0
      sign_in resource_name, resource, :bypass => true
      redirect_to after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      redirect_to backend_perso_path # That's the line I need to change
    end
  end
end

我现在可以重定向到我想要的页面,但我不知道如何表明发生了错误!

【问题讨论】:

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


    【解决方案1】:

    通过Rails Routing from the Outside In 使用“不太优雅”的硬编码user_root 进行简单回答,如How To: Customize the redirect after a user edits their profile 中所述

    #config/routes.rb
    match 'user_root' => redirect("/wherever/you/want")
    

    【讨论】:

    • 如何:自定义重定向...链接提供了对我有帮助的信息。修复需要 5 秒才能实施。谢谢!
    【解决方案2】:

    我没有得到任何有用的答案(不过感谢 Laurent 的解决方案!)所以这是我到目前为止的代码。

    做这项工作,但不是很干净。

    class RegistrationsController < Devise::RegistrationsController
      def update
        # Devise use update_with_password instead of update_attributes.
        # This is the only change we make.
        if resource.update_attributes(params[resource_name])
          set_flash_message :notice, :updated
          # Line below required if using Devise >= 1.2.0
          sign_in resource_name, resource, :bypass => true
          redirect_to after_update_path_for(resource)
        else
          clean_up_passwords(resource)
          redirect_to after_update_path_for(resource), :flash => { :alert => "Error message" }
        end
      end
    
      private
      # Redirect to the URL after modifying Devise resource (Here, our user)
      def after_update_path_for(resource)
        my_path
      end
    end
    

    【讨论】:

      【解决方案3】:

      我没有使用 devise,但我在omniauth 上遇到了类似的问题。 OmniAuth 有一个referer 或 origin 方法,但它不适用于我的帐户创建。所以我所做的是将源页面存储在会话变量中,并在执行操作后检查它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多