My controller is using the default RESTful routes for creating, adding, editing etc
I want to change the default :id to use :guuid. So what I did was:
# routes.rb
resources :posts
# Post Model
class Post < ActiveRecord::Base
def to_param # overridden
guuid
end
end
This works but my modifed REST controller code has something like this
def show
@post = Post.find_by_guuid(params[:id])
@title = "Review"
respond_to do |format|
format.html # show.html.erb
end
end
When I see this this code ..
Post.find_by_guuid(params[:id])
it would seem wrong but it works.
I don’t understand why I can’t write it out like this:
Post.find_by_guuid(params[:guuid])
Why do I still have to pass in the params[:id] when I’m not using it?
Looking for feedback on whether my approach is correct or anything else to consider.
Even though it works it doesn’t always mean it’s right.
Type
rake routesin your console, and check the output of the routes. You’ll see the fragment ‘:id’ in some of them, that’s where theparams[:id]comes from. It’s a rails convention : when you useresourcesin your routes, the parameter is namedid. I don’t know if you can change it (while keepingresources; otherwise you could just go with matching rules), but you shouldn’t anyway : even if it seems not very logic, it actually has sense, once your understand how rails routing works.