简体   繁体   English

重定向到当前页面

[英]redirect to not current page

I have a trobble with redirect in rails 4. I need to redirect to a page specified in a specific situation. 我有一个在rails 4中带有重定向的问题。我需要重定向到在特定情况下指定的页面。 Back redirect does not work , because the page I need to redirect is not the previous page, and I just need to redirect to this page when the user clicks on a link. 向后重定向不起作用,因为我需要重定向的页面不是上一页,并且仅在用户单击链接时才需要重定向到该页面。 The code: View where user click: 代码:查看用户单击的位置:

<li >
  <%= link_to new_admin_wine_path do %>
    <span>
      Cadastrar um novo vinho
    </span>
  <% end %>
</li>

Page to where I need redirect when I click in that link: new_admin_wine_path 单击该链接时需要重定向到的页面:new_admin_wine_path

controller: 控制器:

def store_location
    return unless request.get?
    if (request.path != new_user_session_path &&
        request.path != new_user_registration_path &&
        request.path != new_user_password_path &&
        request.path != edit_user_password_path &&
        request.path != "/:locale/users/confirmation(" &&
        request.path != destroy_user_session_path &&
        !request.xhr?) # don't store ajax calls
      session[:previous_url] = request.fullpath
    end
  end
  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path    
  end

You should look into filters . 您应该查看过滤器 Basically, you're going to want to structure your controller: 基本上,您将要构造控制器:

class someController < ActionController::Base
    after_filter :redirect_method, only: [:whatever, :actions, :you, :want]
    def store_location
        return unless request.get?
        if (request.path != new_user_session_path &&
            request.path != new_user_registration_path &&
            request.path != new_user_password_path &&
            request.path != edit_user_password_path &&
            request.path != "/:locale/users/confirmation(" &&
            request.path != destroy_user_session_path &&
            !request.xhr?) # don't store ajax calls
          session[:previous_url] = request.fullpath
        end
      end
      def after_sign_in_path_for(resource)
        session[:previous_url] || root_path    
      end
     def redirect_method
        redirect_to root_path (or whevever you want to send them) 
     end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM