Given a model like Thread (id, uuid) uuid being a uniquely generated identifier. I want to change the default routes:
edit_thread GET /threads/:id/edit(.:format) {:action=>"edit", :controller=>"threads"}
thread GET /threads/:id(.:format) {:action=&开发者_StackOverflow中文版gt;"show", :controller=>"threads"}
PUT /threads/:id(.:format) {:action=>"update", :controller=>"threads"}
To not use :id but to user :uuid --- How is this made possible in Rails/routes.rb?
Thanks
If I understand correctly, you want to make sure instead of the :id
field, Rails uses the :uuid
field in the routes.
This is quite easy to accomplish, inside your model overrule the to_param
method:
def Thread
def to_param
uuid
end
end
and inside your controller, you will have to write something like:
thread = Thread.find_by_uuid(params[:id])
Hope this helps.
:id
can be changed to whatever you like. Your params
hash gets populated by the name you choose in your routes, so if you changed it to :uuid
you would just need to change your controllers accordingly (o = Model.find_by_uuid(params[:uuid])
)
精彩评论