【问题标题】:Rails, Devise - send user back after submitting formRails,Devise - 提交表单后发回用户
【发布时间】:2016-11-10 12:12:36
【问题描述】:

当用户将产品添加到他的购物车时,他不需要登录。但是当他进入创建的购物车并点击“结帐”时,我会检查他是否已登录:

<%=
    link_to_if(current_user.nil?, "Checkout", new_user_session_path) do
     link_to( "Checkout", new_order_path, method: :get)
   end
%>

所以如果没有登录,我会重定向到 new_user_session_path。问题是登录后他被重定向到 index_path。我希望他被重定向回他的购物车。登录也是一样。

我正在使用 Devise,所以我应该生成 Devise 控制器并从那里开始吗?那应该怎么做呢?

注意:我正在为当前购物车使用模块:

module CurrentCart

  private

    def set_cart
      @cart = Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      @cart = Cart.create
      session[:cart_id] = @cart.id
    end
  end

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

我认为这段代码会对你有所帮助:

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
     #place your route here
  end
end

【讨论】:

  • 我的尝试是:def after_sign_in_path_for(cart) redirect_to @cart and return end 但得到了AbstractController::DoubleRenderError in Devise::SessionsController#create 错误...
  • 但是这个来自那个愤怒的家伙发送的链接的复制/粘贴成功了:after_filter :store_action def store_action return unless request.get? if (request.path != "/users/sign_in" &amp;&amp; request.path != "/users/sign_up" &amp;&amp; request.path != "/users/password/new" &amp;&amp; request.path != "/users/password/edit" &amp;&amp; request.path != "/users/confirmation" &amp;&amp; request.path != "/users/sign_out" &amp;&amp; !request.xhr?) # don't store ajax calls store_location_for(:user, request.fullpath) end end
  • 只是复制这个方法不要使用任何过滤器在这个方法中你把你的代码像你的给出方法这是设计方法
  • 也可以参考这个链接github.com/plataformatec/devise/wiki/…
  • 但没有 :after_filter 它不起作用...无论如何感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多