简体   繁体   中英

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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