Luc*_*cas 10 ruby-on-rails devise ruby-on-rails-3
我想在我的项目中更新/编辑我自己的表单中的设计用户,但问题是我无法在"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 %>
Run Code Online (Sandbox Code Playgroud)
因此用户在该页面上,并更新他的数据.问题,我被重定向到/ users /我试图将其添加到路由:
#config/routes.rb
devise_for :users do
get "users", :to => "backend#perso", :as => :user_root # Rails 3
end
Run Code Online (Sandbox Code Playgroud)
甚至是应用程序控制器:
#/controllers/application_controller.rb
private
def after_update_path_for(resource)
backend_perso_path
end
Run Code Online (Sandbox Code Playgroud)
但仍然没有工作.
感谢有人试图帮助我!
当更新没有生成错误时,我会被重定向到我想要的页面(通过在我的应用程序控制器中添加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
Run Code Online (Sandbox Code Playgroud)
我现在可以重定向到我想要的页面,但我不知道如何显示错误发生了!
通过使用“不太优雅”的硬编码从外部通过 Rails 路由得到简单的答案user_root,如如何:在用户编辑其个人资料后自定义重定向中所述
#config/routes.rb
match 'user_root' => redirect("/wherever/you/want")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5825 次 |
| 最近记录: |