当我的帐户更新失败时,为什么设计我将我重定向到 /users 而不是 /users/edit?
Why does devise me redirect me to /users instead of /users/edit when my account update fails?
问题是这样的:
如果我使用默认的注册控制器,我会被重定向:
- /users/edit 到 root 当用户更新 成功
- /users/edit 到 /users 当更新 失败
因为默认控制器更新操作如下所示:
if resource_updated
sign_in resource_name, resource, bypass: true
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
after_update_path_for
默认设置为 root
如果更新失败我收到设计错误消息和hasError
div错误字段的包装器
在我的 registrations controller
中,在 edit
操作中,我将值归因于一个变量 @myvar={2, 3}
此变量用于registrations_edit
视图
只要用户更新失败,我的@myvar
就会收到错误no method .each for nil
我应该如何声明我的视图变量,以便即使在更新表单失败时也可以访问它们?!
我试过:
无论如何都强制控制器重定向到edit
视图:
if resource_updated
sign_in resource_name, resource, bypass: true
redirect_to edit_user_registration_path
else
clean_up_passwords resource
redirect_to edit_user_registration_path
end
这造成了问题,因为设计验证不再有效
编辑------
我的注册编辑:
def edit
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
end
我的看法:
<% img_paths = Array.new %>
<% @images.each do |image| %>
<%img_paths.push([
'',
"/assets/images/users/#{current_user.id}/original/#{image.split('/').last}",
{'data-img-src'=>"/assets/images/users/#{current_user.id}/med/#{image.split('/').last}"}
])%>
<% end %>
编辑-----
注册控制器:
class Users::RegistrationsController < Devise::RegistrationsController
def edit
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
end
def update
if params[:image_file_path]
ff = File.open("public/"+params[:image_file_path])
resource.image = ff
resource.save!
end
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)
resource_updated = resource.update_with_password(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
respond_with resource
end
end
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :address, :birthday)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :image_file_path, :address, :birthday)
end
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
def after_update_path_for(resource)
edit_user_registration_path
end
end
如果您被重定向,所有控制器实例变量都会被转储,因此除了将您需要的数据保存在通过重定向传播的某个地方之外,没有办法解决这个问题。一个这样的地方是在 flash
消息中(如果你需要一个字符串并且只使用它一次)。
另一种选择是将其存储在 session
中并在另一侧提取。
如果您将 registrations#edit
操作粘贴到问题中,我可以告诉您更多信息。
你可以试试这个;-
def update
if params[:image_file_path]
ff = File.open("public/"+params[:image_file_path])
resource.image = ff
resource.save!
end
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)
resource_updated = resource.update_with_password(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
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
clean_up_passwords resource
respond_with resource
end
end
问题是这样的:
如果我使用默认的注册控制器,我会被重定向:
- /users/edit 到 root 当用户更新 成功
- /users/edit 到 /users 当更新 失败
因为默认控制器更新操作如下所示:
if resource_updated
sign_in resource_name, resource, bypass: true
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
after_update_path_for
默认设置为 root
如果更新失败我收到设计错误消息和hasError
div错误字段的包装器
在我的 registrations controller
中,在 edit
操作中,我将值归因于一个变量 @myvar={2, 3}
此变量用于registrations_edit
视图
只要用户更新失败,我的@myvar
no method .each for nil
我应该如何声明我的视图变量,以便即使在更新表单失败时也可以访问它们?!
我试过:
无论如何都强制控制器重定向到edit
视图:
if resource_updated
sign_in resource_name, resource, bypass: true
redirect_to edit_user_registration_path
else
clean_up_passwords resource
redirect_to edit_user_registration_path
end
这造成了问题,因为设计验证不再有效
编辑------
我的注册编辑:
def edit
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
end
我的看法:
<% img_paths = Array.new %>
<% @images.each do |image| %>
<%img_paths.push([
'',
"/assets/images/users/#{current_user.id}/original/#{image.split('/').last}",
{'data-img-src'=>"/assets/images/users/#{current_user.id}/med/#{image.split('/').last}"}
])%>
<% end %>
编辑-----
注册控制器:
class Users::RegistrationsController < Devise::RegistrationsController
def edit
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
end
def update
if params[:image_file_path]
ff = File.open("public/"+params[:image_file_path])
resource.image = ff
resource.save!
end
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)
resource_updated = resource.update_with_password(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
respond_with resource
end
end
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :address, :birthday)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :image_file_path, :address, :birthday)
end
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
def after_update_path_for(resource)
edit_user_registration_path
end
end
如果您被重定向,所有控制器实例变量都会被转储,因此除了将您需要的数据保存在通过重定向传播的某个地方之外,没有办法解决这个问题。一个这样的地方是在 flash
消息中(如果你需要一个字符串并且只使用它一次)。
另一种选择是将其存储在 session
中并在另一侧提取。
如果您将 registrations#edit
操作粘贴到问题中,我可以告诉您更多信息。
你可以试试这个;-
def update
if params[:image_file_path]
ff = File.open("public/"+params[:image_file_path])
resource.image = ff
resource.save!
end
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)
resource_updated = resource.update_with_password(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
@images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
clean_up_passwords resource
respond_with resource
end
end