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
When the following full path (full path) indicating the directory my_room exists
/my_city/my_home/my_room/i_am_here.html.erb
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
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
/
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 /
**
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. ** **
Is the path handled by Rails an absolute path? Is it a relative path? In thinking The following sites were helpful
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
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 **
Reference:
Use of path method and _url method properly --Qiita
Recommended Posts