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
Related articles
- ruby - nomethoderror at post time with tag function implementation
- ruby - implementation of validation (when blank transmission)
- ruby - [rails] implementation of batch registration form
- ruby - cannot pass id in my page implementation
- ruby - is the test code for the edit screen implementation required?
- ruby on rails implementation of purchase history page when two are purchased at the same time
- ruby on rails implementation of purchase history page when two are purchased at the same time
- ruby - implementation of asynchronous like function in production environment
- ruby - i got the specified record in sql but i can't use it in my app implementation
- ruby - about implementation of tag function in rails
- ruby - [implementation method] how to update the db by clicking the image in the a tag
- ruby on rails - implementation of sales ranking function rails
- ruby - dynamic form implementation in nested_form
- ruby - implementation of posting function with images
- ruby on rails - implementation of validation function
- ruby - [rails] implementation of search function
- ruby - during the implementation of the like function, the posted content is saved in the nameerror database when transitioning
- ruby - [rails] implementation of sending registration completion email
- ruby - rails login mechanism implementation method relationship between corporate id and employee id model
Related questions
- ruby - passing variables using render partial
- ruby - even if each statement is used, the contents of the db cannot be displayed in order and an error occurs
- ruby - i want to generate a random password (mixed alphanumeric characters)
- ruby - sass :: syntaxerror on rails s
- ruby - i want to display new posts i made on the top page only for myself
- ruby - [rails] i want to deploy a rails application using elastic beanstalk
- [javascript] i want to switch the display/non-display of the input form
- ruby - dealing with validation errors when rendering rails apps (definition of instance variables)
- ruby - i get an error with the rails new command
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.