【发布时间】: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