Generating an object's absolute url without html markup - ruby-on-rails

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

Related

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Rails "Show" routes

I'm having trouble creating routes for my Rails app and google is returning mostly results for rails 2-3 from 5 or more years ago.
I'm trying to create a "show" route without using resources, as I need multiple show views.
If I do the following :
get "/parameters/:id" => "parameters#show"
Then if I directly type in the path, like "parameters/12" it will work, but when I run rake routes it doesn't show a path next to the route. And I can't seem to get a path to work for it.
I'm sure I'm forgetting something really obvious here, but I've been at this problem for a few hours and haven't found a solution so I would really appreciate some help.
Because you are defining a custom route, Rails doesn't know how to generate the _url and _path named routes for you automatically.
In order to get the named routes generated, you need to help Rails by specifying the as argument
get '/parameters/:id', to: 'parameters#show', as: 'parameters_show'

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

Rails redirect 301

I have some touble with redirect 301 in my new app. I have to redirect some old urls into the new one.
I entred in my routes file this
match "/traslochi_puglia/index.htm", :to => redirect("/preventivo/90-traslochi-in-puglia")
and it works fine, but I can't understand why this
match "/trasloco_casa_abitazione.htm", :to => redirect("/3-trasloco-casa")
does not work. All the old urls with this pattern "/some_path/page.htm" works fine but not "page.htm". Any hint?
Thanks
If you want us to troubleshoot the specific issue you've outlined in your question, we need to see your entire routes.rb file. Without this information, my first guess is this:
The typical route pattern is /controller/action or /controller/:id/action or some combination thereof. With the pattern you've shown above, and assuming you have no named routes in your routes.rb file, then the route you've provided would point to a controller, but not an action. Therefore your app wouldn't know what action to execute, unless you've specifically created a route called /3-trasloco-casa which looks to me more like a URL to a specific resource, than an action on a controller.
Getting to the source of routing issues can most easily be done with a combination of running rake routes at the command line (which shows you the list of route patterns your app will recognize), and then going further by troubleshooting with route recognition, as outlined in this answer to this question:
Recognize routes in rails console Session

Ruby on rails path helpers

I know this is a minor issue, but why, if you use scaffolding in RoR, can you use lines like 'new_model name here_path' in link tags, but without using scaffolding, I get a NameError? For example, I have a simple address book app that uses basic CRUD operations. I am a RoR beginner but wanted to build an app without scaffolding and these kind of things don't seem to work. I compared my config/routes.rb and app/helpers/* with those in a scaffolded app and they are no different. What am I missing?
One way to check your routes and paths is to run:
rake routes
It outputs all your routes and paths.
Scaffolding sets up resource routes in the routes.rb file. The resource routes are what give you the path and url helpers. When you don't use scaffolding the routes aren't added, you must do it by hand.
Resource Routes can be added like so:
map.resources :models
where :models is the plural name of one of your models.

Resources