Answer the question
In order to leave comments, you need to log in
Is it possible to use calculated values in routes.rb?
Hello!
I have this entry in my routing:
get ':name/preview_:time.jpg' => 'controller#action', as: 'video_preview', name: /[^\/]+/
url_for video_preview_path(name: @view[:file_name], time: File.mtime(@view[:file_name]))
defaults: {time: File.mtime(name)}
Answer the question
In order to leave comments, you need to log in
def my_video_preview_path(file_name)
url_for video_preview_path(name: file_name, time: File.mtime(file_name))
end
I did not find a normal way, however, rails can pass a hash of parameters to redirect and there is access to them. Therefore, you can write something like:
get ':name/preview_:time.jpg' => 'controller#action', as: 'video_preview', name: /[^\/]+/
get ':name' => redirect {|params| "/#{params[:name]}/preview_#{File.mtime(params[:name])}.jpg"}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question