开发者

Rails how to get the file ending in params? .png, .jpg

开发者 https://www.devze.com 2023-04-05 14:37 出处:网络
I am having some problem with my image route, because it does not get the image file ending in params.

I am having some problem with my image route, because it does not get the image file ending in params.

Here is my route:

match '/photographer/ima开发者_JAVA技巧ge/:id/:filename' => 'application#photore'

And my controller:

def photore
  redirect_to "http://s3-eu-west-1.amazonaws.com/mybucket/photographer/image/#{params[:id]}/#{params[:filename]}"
end

I need the filending because else amazon S3 wont load the image.


aNoble is going in the right direction, the Rails format handling is probably eating the extension but it is also probably throwing it away because it isn't .html, .js, or any of the other extensions that it understands. Try setting some constraints on the :filename in your route:

match '/photographer/image/:id/:filename' => 'application#photore', :constraints => { :filename => /.*/ }

That should allow the extension to be left as part of params[:filename].


Assuming you're running Rails 3.1, I believe what you're looking for is params[:format]. This functionality changed from 3.0 to 3.1 but didn't seem to be very well documented.

Try this:

def photore
  redirect_to "http://s3-eu-west-1.amazonaws.com/mybucket/photographer/image/#{params[:id]}/#{params[:filename]}.#{params[:format]}"
end

Or you should be able to pass :format => false to the match method and keep your photore method the same:

match '/photographer/image/:id/:filename' => 'application#photore', :format => false
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号