重定向到非当前页面

时间:2016-06-16 16:19:16

标签: ruby-on-rails redirect

我在rails 4中有重定向功能。 我需要重定向到特定情况下指定的页面。后退重定向不起作用,因为我需要重定向的页面不是上一页,我只需要在用户点击链接时重定向到此页面。 代码: 查看用户点击的位置:

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

点击该链接时我需要重定向的页面: new_admin_wine_path

控制器:

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

1 个答案:

答案 0 :(得分:0)

你应该研究filters。基本上,您将要构建控制器:

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