I'm trying to map the old links from a previous Coldfusion website to the new Rails website. Previously I could do this in the routes.rb file and it would work.
get "/example-products/index.cfm?cid=3", to: redirect(path: "/products")
If I do this, it works.
get "/example-products", to: redirect(path: "/products")
I'm guessing it doesn't like the .cfm
? Can someone fill me in on why this isn't working?
I'm trying to map the old links from a previous Coldfusion website to the new Rails website. Previously I could do this in the routes.rb file and it would work.
get "/example-products/index.cfm?cid=3", to: redirect(path: "/products")
If I do this, it works.
get "/example-products", to: redirect(path: "/products")
I'm guessing it doesn't like the .cfm
? Can someone fill me in on why this isn't working?
1 Answer
Reset to default 2?cid=3
isn't matching properly, if that is a required part of the route you'll have to use :constraints
option to match it:
get "/example-products/index",
constraints: {format: "cfm", params: {"cid" => "3"}},
to: redirect(path: "/products")
For anything more complex you could create a class that has access to a request
object:
class ProductRedirect
def initialize(cid:)
@cid = cid.to_s
end
def matches?(request)
request.params["format"] == "cfm" && request.params["cid"] == @cid
end
end
get "/example-products/index",
constraints: ProductRedirect.new(cid: 3),
to: redirect(path: "/products")
https://api.rubyonrails./classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match