[RUBY] Is the path helper (_path) returning a relative path? Absolute pass? : Rails tutorial memorandum

Is the path helper (_path) returning a relative path? Absolute pass? : Rails tutorial memorandum

When investigating the difference in usage between path helper and url helper ** ”path helper returns relative path, url helper returns absolute path” **

I felt uncomfortable with the notation like that and tried to investigate

As a premise, only in the local environment

When the following full path (full path) indicating the directory my_room exists


Absolute path (= full path)

Path (route) seen from / (root: top level directory) The notation is ** always preceded by / (root) **

/my_city/my_home/my_room/i_am_here.html.erb	#i seen from root_am_here.html.erb path notation

Relative path

Path seen from where you are Notation does not include ** / (root) at the beginning **

my_home/my_room/i_am_here.html.erb 	# my_i seen from city_am_here.html.erb path notation
my_room/i_am_here.html.erb         	# my_i seen from home_am_here.html.erb path notation
../i_am_here.html.erb      	# i_am_here.html.my as seen from erb_home path notation

You can think simply like that

In the Rails development environment?

A mixture of notation with and without /

So far, there are two notations, which have been obscured.

When describing the routing Whereas Rails tutorials add / (root) In Progate, it was written without / (root) (even though / is attached in link_to etc.)

This is easy to understand Even if you describe the routing in the Progate expression without /

  get 'help',      to: 'static_pages#help'	# help -> /The execution result is the same even with help
  get 'about',     to: 'static_pages#about'

If you check the actual routing You can see that / (root) is completed

$ rails routes
	Prefix Verb   URI Pattern   	Controller#Action
	 root GET    /									static_pages#home
	 help GET    /help(.:format)		static_pages#help
  about GET    /about(.:format)		static_pages#about

** Rails will complement it for you, so either notation is fine. The result of internal processing looks like an absolute path based on / **

Does the path helper return a relative path?

Also, the path helper that will help you with the Rails tutorial. The guy that `resource brings

If you refer to the following Similar to the routing description Is the path helper an absolute path based on ** /? Seems to be returning **

2.3 Path and URL helpers Creating resourceful routing makes many helpers available to your application's controller. Take the routing resources: photos as an example.

photos_path returns / photos new_photo_path returns / photos / new edit_photo_path (: id) returns / photos /: id / edit (edit_photo_path (10) returns / photos / 10 / edit) photo_path (: id) returns / photos /: id. (If photo_path (10), / photos / 10 will be returned) Each of these _path helpers has a corresponding _url helper (such as photos_url). The ** _url helper differs in that the current hostname, port number, and path prefix are prepended to _path. ** **

Rails Routing-Rails Guide

Is this an absolute pass? Is it a relative path?

Is the path handled by Rails an absolute path? Is it a relative path? In thinking The following sites were helpful

[For beginners] Explain the difference between absolute paths and relative paths using illustrations! | webliker

In other words, the path / ... that Rails handles is ** Relative route path **, like an absolute path based on the route path (relative, virtual? Route) ** I understood

Since it is expected to move from the development environment to the production environment (In the first example, the home directory has been decided to move to another city directory) It is not a good idea to use full path notation in the development environment And to avoid the vulnerability of relative paths to structural changes Root relative path would be convenient

Is the url helper an absolute path?

If you refer to the above Rails guide again,

Whereas the path helper returns a root relative path The url helper is in the environment in which it is placed (in the production environment, it depends on the server in the production environment) ** Probably getting and returning the full path **


Use of path method and _url method properly --Qiita

Can you explain the difference? Difference between "full path", "absolute path" and "relative path" Flapism

Recommended Posts

Is the path helper (_path) returning a relative path? Absolute pass? : Rails tutorial memorandum
What is a terminal? -Absolute path & relative path-
Rails tutorial memorandum 1
Rails tutorial memorandum 2
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
PATH does not pass ... The cause is the character code
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
A memorandum of the environment variable "JAVA_HOME" path setting procedure
Rails Tutorial Records and Memorandum # 0
The story of the first Rails app refactored with a self-made helper
I made a reply function for the Rails Tutorial extension (Part 1)
What to do if Cloud9 is full in the Rails tutorial
I made a reply function for the Rails Tutorial extension (Part 5):
How to identify the path that is easy to make a mistake
[Self-study] Creating a portfolio (original app) after completing the Rails tutorial