p>When we use resource routing in Rails (paths like "/blogs/new" or "/blogs/2/edit"), we often use path variables such as new_blogs_path. But, when when working in Javascript, we have to hardcode the the paths, i.e. "/blog/new". In this article, we'll cover JsRoutes, an awesome gem that lets us use a similar set of URL helpers in our Javascript code.
Why do we need JsRoutes?
The idea is that you should not have to construct URLs manually in Javascript if a library can do it for you! This becomes evident as soon as you start doing nested routes. For example, consider the following routing configuration:
[ruby] resources :posts do resources :comments end [/ruby]
If we want the path to edit the comment with an ID of 4 that is associated with the post with an ID of 1, it gives "/posts/1/comments/4/edit", which is a mouthful. It is much easier to use the URL helpers supplied by Rails than construct that string in our Javascript.
Continue reading %Rails URL Helpers in Javascript with JsRoutes%
by Dhaivat Pandya via SitePoint
No comments:
Post a Comment