可以在Rails中创建此重定向路由吗? [英] Possible to create this redirecting route in Rails?

查看:73
本文介绍了可以在Rails中创建此重定向路由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Rails应用程序的路由文件中进行重定向?

Is it possible to do a redirect in the routes file of a Rails app?

特别是,我想转发 / j / e /javascripts/embed.js

现在我唯一的方法可以想到的是使用 e 方法创建一个重定向到该控制器的 j 控制器。

Right now the only way I can think to do it is to create a j controller with an e method that redirects to that.

推荐答案

假设rails版本低于3。

Assuming rails version prior to 3.

您可以创建新的 RedirectController 或在现有控制器中添加一个函数,以执行以下操作:

You can create a new RedirectController or tuck a single function away in an existing controller, to do something like the following:

map.js_embed '/j/e',
    :controller => :redirect_controller,
    :action => :some_function,
    :path => "embed"

然后您的函数将执行以下操作:

Then your function would do this:

def some_function
  if params[:path]
    redirect_to "/javascripts/#{params[:path]}.js"
  end
end

或类似的东西。

这篇关于可以在Rails中创建此重定向路由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆