开发者

How to get 'photo'_url(...) to work?

开发者 https://www.devze.com 2023-03-27 03:11 出处:网络
photo_url(Photo.first, :host => \'foobar\') returns http://foobar/photos/2v开发者_Go百科ig0 .

photo_url(Photo.first, :host => 'foobar') returns http://foobar/photos/2v开发者_Go百科ig0 .

How do I do the same thing with something like 'photo'_url(Photo.first, :host => 'foobar')

Do I need to do something like: 'photo'.constantize._url(Photo.first, :host => 'foobar')


You can use send to invoke a method, once you've concatinated the two parts of the string together.

assuming @fn contains the string "photo":

send("#{@fn}_url", Photo.first, :host => 'foobar')

Or, possibly clearer:

send("photo" + "_url", Photo.first, :host => 'foobar')


How about url_for?

http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for

0

精彩评论

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