How to pass params to a block in Rails routes?

*爱你&永不变心* 提交于 2019-12-03 20:28:37

proc returns a Proc, but match is expecting a string. You could try adding .call to have the proc return its value. Though I'm not sure if this will end up calling the proc each time or only when routes is loaded...

EDIT

Seems I was way off-base with my earlier response and comments. Maybe something like this?:

match '/:id', :to => proc { |env|
  id = env["action_dispatch.request.path_parameters"][:id]
  model = Slug.find_by_iid(id).model
  controller = [model.pluralize.camelize,"Controller"].join.constantize
  controller.action("show").call(env)
}

Though this really ought to be defined in a library and included. Perhaps someone knows a better way?

Putting this in your routes seems really hacky. I would recommend creating a Slugs controller, passing this task onto that, and redirecting to the appropriate controller from there. Assuming your other pages use standard RESTful routes, you could do something like this:

Change route to this:

match '/:id', :controller => :slugs, :action => :show

Slugs controller:

def show
  slug = Slug.find_by_iid(params[:id])

  redirect_to send("#{slug.model}_url", params[:id])
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!