当我的帐户更新失败时,为什么设计我会将我重定向到/ users而不是/ users / edit?

时间:2015-03-14 12:35:48

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

问题是这样的:

如果我使用默认注册控制器,我会被重定向:

    用户更新成功后
  • / 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 视图

每当用户更新失败时,我的no method .each for nil

会收到错误@myvar

我应该如何声明我的视图变量,以便即使更新表单失败也可以访问它们?!

我试过了:

强制控制器无论如何都重定向到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

2 个答案:

答案 0 :(得分:1)

你可以试试这个; -

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

答案 1 :(得分:0)

如果你被重定向,所有的控制器实例变量都会被转储,所以除了在通过重定向传播的某个地方保存你需要的数据之外,没办法解决这个问题。其中一个地方是flash消息(如果您需要一个字符串并只使用一次)。

另一种选择是将其存储在session中并在另一侧进行拣选。

如果您在问题中粘贴registrations#edit操作,我可以告诉您更多信息。