【问题标题】:devise after_sign_in_path设计 after_sign_in_path
【发布时间】:2013-01-06 04:48:27
【问题描述】:

我在嵌套模型表单中同时创建位置和用户。我希望用户被带到刚刚创建的位置 ID 的位置显示页面。我该怎么做?

我知道我可以通过将其放入应用程序控制器来获取位置索引

def after_sign_in_path_for(resource)
      locations_path # <- Path you want to redirect the user to.
    end

我试过了

def after_sign_in_path_for(resource)
      location_path(@location) # <- Path you want to redirect the user to.
    end

但它为 id 提供了 nil 值。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails devise routes nested-forms


    【解决方案1】:

    你应该做的就是

    def after_sign_in_path_for(resource)
      location_path(resource) # <- Path you want to redirect the user to.
    end
    

    但如果您的resource 很可能是用户,则更安全的是:

    def after_sign_in_path_for(resource)
      if resource.class == User
        location_path(resource.location) 
      elsif resource.class == Location
        location_path(resource) 
      end
    end
    

    这将处理两种资源类型,并假定您的 User 与创建的 Location 相关联。

    【讨论】:

    • 杰森,就是这样。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多