如果我想匹配 x.gif y.gif ,是否可以将URL传递给 map。 connect 包含两个文件名的可能性:

map.connect "public/images/:name.gif",
  :controller => "static_image_controller",
  :action => "serve"

然后在我的 StaticImageController 中接收 param 作为 params [:name]

class StaticImageController < ApplicationController
  def serve
    image_name = params[:name]
    image = File.read(File.join(Rails.root, image_name))
    send_data image, :type => "image/gif", :disposition => "inline"
  end
end

除了我在这里所做的事情违反了Rais中的约定优于配置的原则,这看起来是否合适?

有帮助吗?

解决方案

map.connect '/public/images/:filename', :filename => /\.gif$/

会这样做。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top