编辑/更新用户时重定向(有错误)

发布于 2024-11-05 18:09:17 字数 3090 浏览 3 评论 0原文

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

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

好的,我的代码是:

#/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

更新

好的,所以我覆盖设计控制器如下: 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

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

I want to update/edit a devise user from my own form in my project, but the problem is that I can't redirect on update on the "request.referer".

I've read that but it didn't work for me: https://github.com/plataformatec/devise/wiki/How-To:-Customize-the-redirect-after-a-user-edits-their-profile
...And the others wiki pages.

Okay, so my code is:

#/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 %>

So the user is on that page, and updates his data. Problem, I get redirected to /users/
I've tried to add that to routes:

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

Or even to the application controller:

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

But still not working.

Thanks for anyone trying to help me out !

Edit

When the update generates no error, I get redirected to the page I want (by adding after_update_path_for in my application controller), but when errors exist, it displays /view/devise/registration/edit.html.erb

Update

Ok, so I overwrited the Devise controller following that:
https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password

So my code in registrations_controller looks like that

#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

I can now redirect to the page I wanted, but I don't know how to show that errors happened !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-11-12 18:09:17

通过 Rails 路由从外到内 使用“不太优雅”的硬编码 < code>user_root 如 如何:用户编辑其个人资料后自定义重定向

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

simple answer via Rails Routing from the Outside In using the "not quite as elegant" hardcoded user_root as described in How To: Customize the redirect after a user edits their profile

#config/routes.rb
match 'user_root' => redirect("/wherever/you/want")
蘸点软妹酱 2024-11-12 18:09:17

我没有得到任何有用的答案(不过还是感谢 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

I didn't get any useful answer (thanks Laurent for your solution though!) so here's my code so far.

Do the job but isn't very clean.

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
过气美图社 2024-11-12 18:09:17

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

I don't use devise, but I've been encountering a similar problem on omniauth. OmniAuth has a referer or origin method, but it wasn't working for me for account creation. So what I did is a dirty hack storing the source page in a session variable, and checking it after the action was performed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文