Rails Get URL Path In View By Route Name - ruby-on-rails

In Django template, I can use
{% url 'pathname' p1 %}
to represent a defined path in url.py. e.g. above will show '/path/to/method/p1'. Is there any method in Rails view (in .html.erb file) to do the same thing?
The "link_to" tag is not useful because it also generate the "<A>" html tag but I only need to get the path of url.
Thanks,

Have you gone through rails routing guide ? if not pls go through as it covers routing very well.
Coming to your question, rails has url helpers.
So for example if your routes file has this route get '/patients/:id', to: 'patients#show' then you could access this url like patient_path(#patient).
You could also define its name like
get '/patients/:id', to: 'patients#show', as: 'patient' where as: patient is the user defined name that could be used in the views to access this url like patient_path. You append _path to get the relative url.

Related

Rails routes: redirect a wild card route

I have a website built in Ruby on Rails and a blog built in WordPress. The url for the main site will be example.com and the url for the blog is blog.example.com. Because of the way search engines index sites and the preference for a directory blog as opposed to a subdomain blog, I want to implement a permanent redirect so that example.com/blog/anything will redirect to blog.example.com/anything regardless of how many slashes or parameters the url contains. So even example.com/blog/a/b/c/d/e?google=true should redirect to blog.example.com/a/b/c/d/e?google=true
So far the following works if there is only one directory after blog:
get '/blog/:what', to: redirect('http://blog.codeundercover.com/%{what}')
I, however, need this to work regardless of what text comes after /blog. How do I do this?
Your route is not using a wildcard route. Instead, it's what the Rails Routing Guide refers to as a Static Segment
You'll want to use Wildcard Globbing instead:
get '/blog/*what', to: redirect('http://blog.codeundercover.com/%{what}'), constraints: { what: /.*/ }
get "/blog(/*what)", to: redirect("http://blog.codeundercover.com/%{what}")

In Rails, is <resource>_path automatically available?

This seems like a basic question, but does Rails automatically interpret <resource>_path as a route after you have created the route (by understanding the additional _path)?
Creating a resourceful route will also expose a number of helpers to the controllers and views in your application. In the case of resources :photos:
photos_path returns /photos
new_photo_path returns /photos/new
Each of these helpers has a corresponding _url helper (such as photos_url) which returns the same path prefixed with the current host, port and path prefix.
Yes, but it only works on the controllers and views, not in the models.
Resources generates named routes for use in controllers and views.
Routes can be named for easy reference. And named helpers are available for named routes. For example:
If you define a named route:
root to: 'blogs#index'
Then you can use named helpers _url & _path:
root_url # => 'http://www.example.com/'
root_path # => '/'
_path provides a site root relative path which is used most frequently. _url provides the full url including protocol and server name and is used for generating urls for external use (like you need a shareable link).

Rails - Find out where a url-helper points

In rails, when I have a form with a path-helper, for instance create_admin_shop_voucher_path and I want to find out what controller action it points to, I look in routes.rb:
rake routes | grep 'create_admin_shop_voucher'
But how do I do the same for a url-helper, for instance passed_url ?
Lets say you have this route in your RAILS_ROOT/config/routes.rb file
get "passed" => "passed#index", :as => :passed
If you call passed_url
rails will return the whole url. For instance http://localhost:3000/passed
if you call passed_path
rails will return the relative path /passed
*_url helper generates a URL that includes the protocol and host
name. The *_path helper generates only the path portion.
So, the its the same route in your RAILS_ROOT/config/routes.rb file
In your case you can call create_admin_shop_voucher_url as well as create_admin_shop_voucher_path.
Mostly you should use the _path flavor. If you need to spec the host or
protocol (like for talking to another app or service), then use the _url
flavor.
I use _url when i want to pass for instance a subdomain to a link.

Rails: Map of path helper methods to URL formats?

rake routes gives me a mapping of "controllerName#method" to URL path format
Rails.application.routes.named_routes.helpers gives me a list of all helper methods.
Is there something that can give me a mapping of all helper methods to URL path formats? I'm not very experienced with Rails so something that tells me what URL the Rails helper methods actually generate would be EXTREMELEY awesome.
I'm confused what you're asking? I think rake routes already tells you what you're looking for. Entries are in the format:
route_name HTTP_VERB path controller#action
Appending _path or _url to the route_name will give you a url helper. Ex. in my app I have
scenario GET /scenarios/:id(.:format) scenarios#show
And I can get a path or url to it by using scenario_path(scenario) or scenario_url(scenario) respectively.

Rails Routing and View Helper Names

I have this route defined in my routes.rb:
matc "articles/:id/vote/up" => "articles#vote", :liked=>true
The rake for this route is:
/articles/:id/vote/up(.:format) jokes#vote {:liked=>true}
With the first column being blank.
My question is, what would the route helper name be for this link?
For example articles_vote_up_path(:id).
Is there an uglier way to link to this path?
How can I find out?
I'm thinking I misunderstood something, please guide me in the correct direction in that case.
Thank You
you need to pass an :as option to use a named route
match "articles/:id/vote/up" => "articles#vote", :liked=>true, as: :article_vote_up
which generates an article_vote_up_path
When you run rake routes, you can add _path to the whatever's in the first column on the left, and that's what the route url helper is for that link. For example, here's a line from rake routes on one of my projects:
subscriptions GET /subscriptions(.:format) subscriptions#index
The url helper for this url would be subscriptions_path.

Resources