How to translate a route scope in Rails? - ruby-on-rails

I'm trying to translate the routes in a Rails application. I tried with i18n_routes, but I had some problems with translating actions that didn't depend on a resource. In the end, I solved it without using i18n_routes, as explained here.
The issue now is that I don't find the way to translate scopes. This is my routes.rb:
scope "sport" do
translated_named_route 'it_is_healthy', 'sport#it_is_healthy'
translated_named_route 'it_is_social', 'sport#it_is_social'
end
How to translate sport? I tried with i18n_routes again, but I don't see how to translate the scope. Thanks!

If you are open to using a gem for this, I would recommend using
https://github.com/francesc/rails-translate-routes
The readme is fairly comprehensive and it definitely covers namespaces and scopes in the routes as well.

Related

API for methods on Ruby on Rails

I am doing Hartl's tutorial. I often don't know whether some methods mentioned in the tutorial are defined in Rails or in the code in the tutorial. I have problem finding methods when learning Rails. Does anyone know where I could find the API for:
edit_account_activation_url(#user.activation_token, email: #user.email
in Listing 11.13? Can someone advise me how I could finding the methods' API in general?
It is url helper, it's automatically created from routes.rb. You can find more in Rails guides
In routes you have a line resources :account_activations, only: [:edit]
According to url generation rules it produces 1 route:
http://www.example.com/account_activations/:id/edit
Helper edit_account_activation_url takes id as a first parameter, it is #user.activation_token in your case. As a second parameter it takes query hash (we can omit {} for last hash parameter) - it is { email: #user.email }. That how it works
You can solve the problem of "not finding the methods' API" by reading the guide together with the tutorial
Can someone advise me how I could finding the methods' API in general?
Good question. I use Pry.
[3] pry(main)> show-source app.users_path
From: /home/sergio/.gem/ruby/2.4.4/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb # line 342:
Owner: #<Module:0x0000563dd616fc28>
Visibility: public
Number of lines: 5
define_method(name) do |*args|
options = nil
options = args.pop if args.last.is_a? Hash
helper.call self, args, options
end
In this particular case, it's much less helpful than the routing guide mentioned by #Vasilisa. But at least it gives you a hint. And it's very useful generally. Especially when you're dealing with, say, a gem/library which uses inheritance/mixins heavily and you're never quite sure which of 30 call implementations you're invoking here. This approach shows exact location and source code.
A good source of information is APIdock. This site allows you to do a search against Ruby or Rails for API information. What is useful is that users will add additional code snippets or comments about the methods on the API
The other source of just Rails API is at Ruby on Rails API. This typically gives you the latest API information. Comparatively the APIdock site will show the different Rails release version on the version timeline at the top of the page

How do I override _path helpers in rails properly?

I have a model called resource in my rails app and in need of modifying the return value of the helper *resource_path*, I've read some docs and SO Q/A and they're generally suggesting put the customized helper in *app/helpers/application_helper.rb*. The thing bothers me is that what do I do with the old auto generated helper? should I do something like
undef resource_path
before I go ahead and write my own helper? Currently I have a *resource_path* method defined within ApplicationHelper, interestingly when I open rails console, app.resource_path and helper.resource_path giving me different result.
Also, I'd like to hear a deeper explanation on how *_path* helpers implemented and how they are related to *link_to* helper, as the source code are kinda hard to read with so many meta programming techniques involved
Yes, you are able to do it like following:
resources :photos, as: 'images'
in your config/routes.rb file.
More details you can find here http://guides.rubyonrails.org/routing.html especially 4.3 Overriding the Named Helpers

Generating an object's absolute url without html markup

Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2

How to generate documentation with Yard for Rails views?

I have just started using Yard to generate my docs and whilst it works great for my models and controllers, I can't seem to get it to generate anything for the views
I've tried a variety of different ways to get it to work. Currently I've set up a rake task to generate the docs that looks something like this
YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'app/**/*.rb', 'app/views/**/*.erb']
t.options = []
end
The last path there, I believe, should get it to include my views but when I look at the docs, there's no mention of any of these files at all. Any ideas why?
Cheers
Template files are not expected to contain Ruby methods or business logic. What kind of documentation would you like to generate?

New to Rails. Doubt in Big URL Routing

I have just started learning ruby on rails. I have a doubt wrt routing.
Default Routing in Rails is :controller/:action/:id
It works really fine for the example lets say example.com/publisher/author/book_name
Could you tell me how do you work with something very big like this site
http://www.telegraph.co.uk/sport/football/leagues/premierleague/chelsea/
Could you let me understand about the various controllers, actions, ids for the above mentioned url and how to code controller, models so as to achieve this.
Could you suggest me some good tutorials when dealing with this big urls.
Looking forward for your help
Thanks in advance
Gautam
This is achieved using nested resources (read or google for "rails restful routes". In your case it could look something like this:
map.resources :sports do |sport|
sport.resources :leagues do |league|
league.resources :team
# probably more nested routes for members or sponsors or whatever...
end
end
end
You can also view your defined routes with rake task:
$ rake routes
This RailsCasts episode also covers some basics on restful routing with nested resources.
The routing engine can handle URLs of arbitrary size. It all depends on what your spec is. For that it would be:
map.sport_league_team '/sport/:sport/leagues/:league/:team'
What controller you route that to is the important part. This is then called like:
<%= link_to("Chelsea", sport_league_team_path('football', 'premierleague', 'chelsea') %>
You can always examine what routes are defined with:
rake routes
The Rails Routing from the Outside In guide is a good place to start.

Resources