Shp*_*ord 44 redirect routes ruby-on-rails
是否可以在Rails应用程序的路径文件中进行重定向?
具体来说,我想转发/j/e给/javascripts/embed.js
现在,我能想到的唯一方法是使用重定向到该方法的方法创建一个j控制器e.
Ste*_*oka 161
使用Rails 3,您可以在routes.rb文件中重定向.
get '/stories', to: redirect('/posts')
Run Code Online (Sandbox Code Playgroud)
在Rails 4中:(感谢@dennis)
match "/posts/github" => redirect("http://github.com/rails.atom")
Run Code Online (Sandbox Code Playgroud)
假设3之前的rails版本.
您可以RedirectController在现有控制器中创建新功能或将单个功能折叠起来,执行以下操作:
map.js_embed '/j/e',
:controller => :redirect_controller,
:action => :some_function,
:path => "embed"
Run Code Online (Sandbox Code Playgroud)
然后你的功能会这样做:
def some_function
if params[:path]
redirect_to "/javascripts/#{params[:path]}.js"
end
end
Run Code Online (Sandbox Code Playgroud)
或者那种效果的东西.