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

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

问题描述

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

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

具体来说,我想将 /j/e 转发到 /javascripts/embed.js

Specifically, I'd like to forward /j/e to /javascripts/embed.js

现在我能想到的唯一方法是创建一个 j 控制器,并带有一个重定向到该控制器的 e 方法.

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

或达到这种效果的东西.

or something for that effect.

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

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