A
A
Anton Ivanov2015-03-15 01:39:56
Ruby on Rails
Anton Ivanov, 2015-03-15 01:39:56

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: /[^\/]+/

if i call in view
url_for video_preview_path(name: @view[:file_name], time: File.mtime(@view[:file_name]))

, then everything is ok. I get a link like 1.mp4/preview_ 1426372655.jpg
but I don't want to constantly use two parameters, one of which always depends on the other.
I want to prescribe something like this in the routing, but it doesn’t work like that, since the name variable is not defined. Is there an easy way out of this situation? Thank you! defaults: {time: File.mtime(name)}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2015-03-16
@Fly3110

def my_video_preview_path(file_name)
  url_for video_preview_path(name: file_name, time: File.mtime(file_name))
end

Well, if you need real street magic, then take a look at the alias function from the ruby ​​library

T
thepry, 2015-03-15
@thepry

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"}

Although all these are crutches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question