【问题标题】:How to redirect_to in custom devise controller?如何在自定义设计控制器中重定向_to?
【发布时间】:2020-12-19 10:15:00
【问题描述】:

我已经设计了自定义控制器(用户控制器),就像 here 解释的那样。

我像这样自定义了一些控制器逻辑

def create

  super do |user|
    @patient = user.build_patient
    @patient.save
  end

end

所有这些都可以正常工作。问题是当我尝试提供自定义redirect_to 时,我看到了

def create

  super do |user|
    @patient = user.build_patient
    @patient.save
  end

  redirect_to physicians_path
end

Solutions 到一般问题(意外或其他)> 1 redirect_to 在控制器内部不起作用,因为 i)我不能影响第一个(因为它是设计的一部分),并且 ii ) 我不知道如何告诉它忽略第一个并使用我指定的那个

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-6


    【解决方案1】:

    我不会接受这个答案,因为我确信有更好的方法,但无论如何都是这样:

    1. 从设计代码中的relevant controller 复制代码
    2. 随意编辑
    3. 我没有使用super(我认为最好使用,但我不知道如何使用)

    所以我只是添加了这个

    def update
    
      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 = update_resource(resource, account_update_params)
      yield resource if block_given?
      if resource_updated
        set_flash_message_for_update(resource, prev_unconfirmed_email)
        bypass_sign_in resource, scope: resource_name if sign_in_after_change_password?
    
        # respond_with resource, location: after_update_path_for(resource)
        redirect_to edit_user_registration_path
      else
        clean_up_passwords resource
        set_minimum_password_length
        respond_with resource
      end
    
    end
    

    请注意,与设计代码的唯一区别是注释掉这个

        # respond_with resource, location: after_update_path_for(resource)
    

    并将其添加到它的位置

        redirect_to edit_user_registration_path
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多