Rails routing: Treating hardcoded path as a parameter? - ruby-on-rails

I'm new to Rails, and after reading some of the Rails Routing documentation, I still can't figure out why my path is being treated as such:
With the following examples, Rails complains that it is unable to find a Movie whose id is equal to "inception", even though I am trying to pass along an id parameter.
(The controller#action to which I am routing is of the resources-type show).
routes.rb
1: get 'movies/inception' => "movies#show", :id => 6
2: get 'movies/inception' => 'movies#show', defaults: {id: 6}
(These two lines are obviously tried separately.)
Error
1:Couldn't find Movie with id=inception
2: app/controllers/movies_controller.rb:16:inshow'
3: {"id"=>"inception"}
Why is the inception-part of the matched url treated as a variable even when I'm not prefixing it with : and how do I make a hardcoded url point to a object#show-action with a hardcoded id?

Looks like you taked into issue with routes priority:
Not all routes are created equally. Routes have priority defined by the order of appearance of the routes in the config/routes.rb file. The priority goes from top to bottom. The last route in that file is at the lowest priority and will be applied last. If no route matches, 404 is returned.
So, I suppose, that you had defined
resources :movies
before
get 'movies/inception' => "movies#show", :id => 6
get 'movies/inception' => 'movies#show', defaults: {id: 6}
And your routes were not overwritted.
Just see your last question. Uses this route form, instead of yours:
get 'movies/inception/(:id)' => 'movies#show', defaults: {id: 6}

My guess is you have a line something like
resources :movies
If this is the case, then it is creating the standard get show route of movies/:id
If this line is above your hard-coded route then it would take precedence. The lines at the top of the routes file take precedence over anything below it
A couple of things that may help you on your rails journey. From the command line, run
rake routes
That will spit out all available routes
Something you may want to consider if you are looking to do this:
Check out the friendly_id gem
"It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models." such as /movies/inception

Related

Rails uncountable model name, no route matches get name_index

I have a model with uncountable name - class Equipment and in this article (https://markembling.info/2011/06/uncountable-nouns-rails-3-resource-routing) I found that in such cases we get into problems while trying to get model's index path. So article provides tips how to use inflection rules. However, I believe word 'Equipment', just like 'person' is already understood by Rails and I dont even need to define inflection rule, since I still get this path:
equipment_index GET /equipment(.:format) equipment#index
But, for some reason, after I navigate to localhost:3000/equipment_index, I get
No route matches [GET] "/equipment_index"
All other paths works (like localhost:3000/equipment).
Any ideas whats going on..?
p.s. please do not write how to add a custom path. I hope to solve this in the Rails way - convention over configuration. Thanks.
routes:
equipment_index GET /equipment(.:format) equipment#index
POST /equipment(.:format) equipment#create
new_equipment GET /equipment/new(.:format) equipment#new
edit_equipment GET /equipment/:id/edit(.:format) equipment#edit
equipment GET /equipment/:id(.:format) equipment#show
PATCH /equipment/:id(.:format) equipment#update
PUT /equipment/:id(.:format) equipment#update
DELETE /equipment/:id(.:format) equipment#destroy
routes.rb:
resources :users do
member do
get 'generate_raport'
end
end
resources :client_users
resources :clients
devise_for :users, skip: [:registrations]
resources :equipment
root to: 'static#homepage'
equipment_index is a named route, not a url string. The url string that corresponds to this named route is in this part:
GET /equipment(.:format)
When you say:
equipment_index GET /equipment(.:format) equipment#index
you are really saying that equipment_index is a named route (an alias so to say) for the actual url route localhost:3000/equipment. The last part that says:
equipment#index
just says that your request will be routed through the equipment controller and the corresponding index action.
Solution
You can simply navigate to localhost:3000/equipment to get to the index page for your equipment controller.
For example, you would link to this page using a rails link_to helper and the named route discussed above like this:
link_to "My index path", equipment_index_path
Follow up on comments
change add the following line to your routes.rb file directly after the line that contains resources :equipment. It would now look like:
resources :equipment
get 'equipment', to: 'equipment#index', as: 'equipment'
This is convention over configuration!
You're simply reading the output of rake routes wrong or have the wrong expectations about how its supposed to work. The first column is just the name of the route which is primarily used for creating path helpers. The actual paths are in the third column*.
equipment_index_path() # /equipment
equipment_path(1) # /equipment/1
equipment_path() # error due to missing id param
Since equipment is an uncountable noun Rails cleverly avoids an issue where the generated path helpers would be ambiguous - equipment_path could potentially lead to either the index action or the show action. Regular countable nouns don't have this issue so the _index postfix is not usually needed.
# no ambiguity
cats_path() # /cats
cat_path(1) # /cats/1
While you could argue that rails in that case should use the presence of the id param to differentiate that is not how its built and could mask bugs where you pass nil instead of a record.

Error: `Couldn't find Course with 'id'=` when visiting `/courses/show`

I'm new to Rails, and I'm setting up models/controllers for Course and some other models.
When I visit the /courses/show URL in my browser I get the following error:
Couldn't find Course with 'id'=
Screenshot here.
Here's the relevant line from my rake routes and routes.rb:
rake routes
courses_show GET /courses/show(.:format) courses#show
config/routes.rb
get 'courses/show'
You have specified the four routes without any :id parameter, I don't know why you would expect them to have an :id parameter.
I'd recommend that you read the Rails guide on routing and also read the comments in the generated config/routes.rb, in that file you'll see comments like this:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
So, extrapolating that to your example you might end up with:
get 'courses/:id' => 'courses#show'
The example that follows that one shows how to add a named route helper using the :as option:
get 'courses/:id' => 'courses#show', as: :courses_show
Something you'll also see when you read the guide or the comments is that you can use the resources helper to create standard restful routes.

Why did Ruby on Rails' URL Helper put a period in my URL?

I have the following code in my view (RoR 4):
tbody
- #order_submissions.each do |order_submission|
tr
td = order_submission.id
td.table-actions
span = link_to "Show", order_submissions_path(order_submission.id)
td = order_submission.id
successfully displays as the ID number (533ab7337764690d6d000000)
But...
order_submissions_path(order_submission.id)
Creates a URL that comes out as:
order_submissions.533ab7337764690d6d000000
I want it to be
order_submissions/533ab7337764690d6d000000
Where did that period come from?
This is my route:
get 'order_submissions/:id' => 'order_submissions#show'
And when I run rake routes I get:
GET /order_submissions/:id(.:format) order_submissions#show
The (.:format) is probably what's messing it up but I don't know why. I just want it to put a slash in there.
If I change my code to this it fixes it:
span = link_to "Show", order_submissions_path + '/' + order_submission.id
But that's a really, really stupid workaround.
EDIT: Here are my routes:
get 'order_submissions' => 'order_submissions#index'
get 'order_submissions/new' => 'order_submissions#new'
post 'order_submissions' => 'order_submissions#create'
get 'order_submissions/:id' => 'order_submissions#show'
get 'order_submissions/:id/edit' => 'order_submissions#edit'
patch 'order_submissions/:id' => 'order_submissions#update'
get 'order_submissions/:id/delete' => 'order_submissions#delete'
delete 'order_submissions/:id' => 'order_submissions#destroy'
The order_submissions_path (plural) points to /order_submissions. It takes two arguments, the first being the format for the request (e.g. html). Your ID is being passed in for this argument, leading to the resulting URL you're seeing.
You actually want the singular path helper, order_submission_path, which accepts an ID as the first argument.
Because it should be a singular form:
order_submission_path(order_submission.id)
Not
order_submissions_path(order_submission.id)
order_submissions_path points onto index action. You can also remove id from the end.
UPDATE:
Just notice you route file. Do you have any resources defined there? The route you posted wouldn't generate any url_helper as you dind't specify route name (most likely this entry is obsolete, as I expect there is resources :order_submissions somewhere there as well).
You don't get a named route by default. The route you showed from rake routes doesn't list a named route, for example.
GET /order_submissions/:id(.:format) order_submissions#show
Normally, you'd see the named route in front of GET there.
So you can define it yourself and then your route will work:
get 'order_submissions/:id' => 'order_submissions#show', as: :order_submission
Notice the as: :order_submission bit. Now, order_submission_path(order_submission.id) will work. (Note: .id is superfluous if your order_submission responds to to_path and returns id.)
I'm guessing you have another route in your rake routes output that uses the named route you supplied and that doesn't use /:id. Perhaps your index route?

Rails Routing: How to route to a specific controller/action/id?

I think this should be a simple question. I would like to route a URL of the form:
http://mywebsite.com/my_special_tag
to a specific controller/action pair with a specific id, e.g.
http://mywebsite.com/user/post/13
(where 13 is the id I want to pass into the controller/action).
Note also that I don't want to redirect, just want to render.
Is there an easy way to do this in Rails 3?
Thanks!
Assuming your know specifically which URL you want to have rendered to, you can explicitly match to the target in your routes:
# config/routes.rb
match 'my_special_tag' => 'user#post', :defaults => {:id => 13}
Note that this necessarily requires that you have a post action within a UserController (note that the names are singular, as they are in your desired path).
You simply need a table like this:
tags table:
id | post_id | tag
-------------------
1 | 13 | my_special_tag
And then you need a before_filter, that takes that tag and checks the database for the post_id.
You'd also need a wildcard route that looks something like this: Rails 3.1 - in routes.rb, using the wildcard, match '*' is not catching any routes
Start with that and then let us know when you have more specific questions and we can help you better.
If you check your routes you should get the syntax for a user post route, if you have defined the nested route correctly.
To alias the route, you can define a specific id for a particular match like so:
match '/special_tag' => 'posts#index', :defaults => { :id => '5' }
Update
To clarify, the above should only be used for a couple of specific pages you'd like to alias a route for. If you want to provide a more user friendly link to each user post page, I'd recommend something like https://github.com/norman/friendly_id which allows you to have routes like
http://example.com/states/washington
instead of:
http://example.com/states/4323454

Shallow routing in Rails 3

Having a rough time with my routes in my rails 3 app, I want to have shallow routes like this:
/san-francisco/union-square
But my router insists on having them like so:
/cities/san-francisco/neighborhoods/union-square
I've used this for my routes.rb
shallow do
resources :cities do
resources :neighborhoods do
resources :locations
end
end
end
But still I have this:
city_neighborhood_locations GET /cities/:city_id/neighborhoods/:neighborhood_id/locations(.:format)
Shouldn't it look like:
city_neighborhood_locations GET /:city_id/:neighborhood_id/:id(.:format)
I'm not sure how to fix this, additionally I'm not sure what I'm doing wrong with my links, I want to be able to use the syntax:
link_to neighborhood.name, [:city, neighborhood]
but that seems to invert the :id, and :neighborhood_id when the request comes to the controller, any help on this would be really really helpful!
What you're looking for is not typically known as "shallow routing".
Shallow routing, as Rails defines it, would look like this:
city_neighborhoods GET /cities/:city_id/neighborhoods
new_city_neighborhood GET /cities/:city_id/neighborhoods/new
(create_city_neighborhood) POST /cities/:city_id/neighborhoods
neighborhood GET /neighborhoods/:neighborhood_id
Without shallow routing, that last route would be:
city_neighborhood GET /cities/:city_id/neighborhoods/:neighborhood_id
Shallow routing lets you nest a resource underneath another resource (neighborhood under city in this case), but gives you absolute/un-nested routes for the nested resource when the nesting isn't necessary.
This makes sense when you're referencing the nested resource with unique identifiers that are not dependent on the ID of the parent resource. In your example, that's not true (there's potentially a "Union Square" outside of San Francisco; there's definitely going to be duplicates like "Chinatown"), so you probably do not want shallow routes for this case.
What you're wanting is positionally-dependent routing, where the type of resource is assumed/fixed depending on where it appears in the URL. (For instance, you couldn't have anything other than a "neighborhood" follow a "city" under the scheme you outlined.)
I don't think the Rails resource(s) commands will support that by default, but you could probably do it with manual match commands. This is off the top of my head:
match ":city_id", :controller => "cities", :action => "show"
match ":city_id/:neighborhood_id", :controller => "neighborhoods", :action => "show"
This is still RESTful/resource-based, it's just not using the standard Rails way of naming routes.

Resources