Home>
About implementing friendly forwarding

I'm currently creating a portfolio in rails, but I get the following error:
I think it's probably because I haven't implemented friendly forwarding, but I feel like I have all the files and code I need after a lot of research.
Please let me know if you have any solution.

Failures:
  1) Profile editing For authorized users The response is displayed normally (+ friendly forwarding)
     Failure/Error: expect (response) .to redirect_to edit_user_url (user)
       Expected response to be a redirect to<http://www.example.com/users/17183/edit>but was a redirect to<http://www.example.com/users/17183>.
       Expected "http://www.example.com/users/17183/edit" to be === "http://www.example.com/users/17183".
     # ./spec/requests/users_edit_spec.rb:11: in `block (3 levels) in<top (required)>'
     # -e: 1: in `<main>'
  2) Edit profile For users who are not logged in Redirect to the login screen
     Failure/Error: expect (response) .to redirect_to login_path
       Expected response to be a redirect to<http://www.example.com/login>but was a redirect to<http://www.example.com/>.
       Expected "http://www.example.com/login" to be === "http://www.example.com/".
     # ./spec/requests/users_edit_spec.rb:26:in `block (3 levels) in<top (required)>'
     # -e: 1: in `<main>'
Corresponding source code
[users_edit_spec.rb]
require "rails_helper"
RSpec.describe "Edit Profile", type :: request do
  let! (: user) {create (: user)}
  let! (: other_user) {create (: user)}
  context "For authorized users" do
    it "Response is displayed normally (+ Friendly forwarding)" do
      get edit_user_path (user)
      login_for_request (user)
      expect (response) .to redirect_to edit_user_url (user)
      patch user_path (user), params: {user: {name: "Example User",
                                               email: "[email protected]",
                                               introduction: "Nice to meet you",
                                               sex: "male"}}
      follow_redirect!
      expect (response) .to render_template ('users/show')
    end
  end
  context "For users who are not logged in" do
    it "Redirect to login screen" do
      #Edit
      get edit_user_path (user)
      expect (response) .to have_http_status "302"
      expect (response) .to redirect_to login_path
      # Update
      patch user_path (user), params: {user: {name: user.name,
                                               email: user.email}}
      expect (response) .to have_http_status "302"
      expect (response) .to redirect_to login_path
    end
  end
  context "For users with different accounts" do
    it "Redirect to home screen" do
      #Edit
      login_for_request (other_user)
      get edit_user_path (user)
      expect (response) .to have_http_status "302"
      expect (response) .to redirect_to root_path
      # Update
      patch user_path (user), params: {user: {name: user.name,
                                               email: user.email}}
      expect (response) .to have_http_status "302"
      expect (response) .to redirect_to root_path
    end
  end
end
[users_controller.rb]
class UsersController</pre>
<pre><code data-language = "Ruby">[sessions_helper.rb]
module SessionsHelper
  def log_in (user)
    session [: user_id] = user.id
  end
  # Make the user's session persistent
  def remember (user)
    user.remember
    cookies.permanent.signed [: user_id] = user.id
    cookies.permanent [: remember_token] = user.remember_token
  end
  # Returns true if the passed user is a logged-in user
  def current_user? (user)
    user == current_user
  end
  #Returns the user corresponding to the memory token cookie
  def current_user
    if (user_id = session [: user_id])
      @current_user || = User.find_by (id: user_id)
    elsif (user_id = cookies.signed [: user_id])
      user = User.find_by (id: user_id)
      if user&&user.authenticated? (cookies [: remember_token])
        log_in user
        @current_user = user
      end
    end
  end
  def logged_in?
    ! current_user.nil?
  end
  #Destroy a persistent session
  def forget (user)
    user.forget
    cookies.delete (: user_id)cookies.delete (: remember_token)
  end
  def log_out
    forget (current_user)
    session.delete (: user_id)
    @current_user = nil
  end
  # Redirect to the memorized URL (or default value)
  def redirect_back_or (default)
    redirect_to (session [: forwarding_url] || default)
    session.delete (: forwarding_url)
  end
  #Remember the URL you tried to access
  def store_location
    session [: forwarding_url] = request.original_url if request.get?
  end
end
[sessions_controller.rb]
class SessionsController
[application_controller.rb]
class ApplicationController
What I tried

I researched and confirmed various things needed to implement friendly forwarding on the net.

  • Answer # 1

    Was self resolved!

    In conclusion, the order of "before_action" in users_controller.rb was reversed.
    If the user is not logged in with "logged_action_user", he will jump to the login page (/ login), but since "correct_user" has come first, he will jump to the root page (/) before jumping to the login page. It was.