[英]How do I create a user-defined redirect in ruby on rails?
I have created a model called LandingPage, and the current route is something like: 我创建了一个名为LandingPage的模型,当前路由如下:
www.domain.com/landing_pages/1
Or something like that. 或类似的东西。 I will have several of these, so landing_page where id = 1...n. 我会有几个,所以landing_page,其中id = 1 ... n。
However, when someone creates the landing page, I would like them to be able to define an attribute for the model, such as "superbowl" to redirect to landing_page/1, "nice" to landing_page/2, etc. 但是,当有人创建着陆页时,我希望他们能够为模型定义属性,例如“superbowl”重定向到landing_page / 1,“nice”到landing_page / 2等。
This would allow the user to define the landing page as: 这将允许用户将登录页面定义为:
subdomian.domain.com/superbowl
which would resolve to 哪个会解决
www.domain.com/landing_pages/1/
How do I do that? 我怎么做? Each landing_page has an attribute 'shortname' which ties to its specific landing_page. 每个landing_page都有一个属性“shortname”,该属性与其特定的landing_page相关联。
I am on Rails 2.3.8. 我在Rails 2.3.8上。 My hunch is in the routes to loop through the available shortnames, but not sure. 我的预感是在循环可用的短名称的路线,但不确定。
8 def show
9
10 @landing_page = LandingPage.where(:name => params[:name]).first
11 redirect_to landing_page_path(@landing_page)
12
13 #@landing_page = LandingPage.find(params[:id])
14 @redcloth_landing_page = RedCloth.new(@landing_page.message).to_html
15 form = "<div id='form'>" << @landing_page.form << "</div>"
16
17 @redcloth_landing_page.gsub!("{Form}",form)
18
19 render :layout => false
20 end
Taking your comments into account, you need a RedirectsController, and in your routes.rb file, you would have: 考虑到您的意见,您需要一个RedirectsController,并在您的routes.rb文件中,您将拥有:
map.connect '/:name', :controller => 'redirects', :action => 'show'
You need to make sure that this route comes after your resource routes. 您需要确保此路由位于资源路由之后。 In your RedirectsController, the show method would be something like: 在您的RedirectsController中,show方法将类似于:
def show
@landing_page = LandingPage.first(:conditions => {:name => params[:name]})
redirect_to @landing_page
end
I'm not sure what you're trying to achieve at www.domain.com/landing_pages/1/, but using your code as an example, you should have the following in your LandingPagesController show action: 我不确定您在www.domain.com/landing_pages/1/上想要实现的目标,但是使用您的代码作为示例,您应该在LandingPagesController show action中有以下内容:
def show
@landing_page = LandingPage.find(params[:id])
@redcloth_landing_page = RedCloth.new(@landing_page.message).to_html
form = "<div id='form'>" << @landing_page.form << "</div>"
@redcloth_landing_page.gsub!("{Form}",form)
render :layout => false
end
However, this solution is not as good as before because, unless I am missing it, I do not know that you can constrain a route to a subdomain in 2.3.8. 但是,这个解决方案并不像以前那么好,因为除非我错过它,否则我不知道你可以在2.3.8中约束到子域的路由。 Without constraining to subdomains, you would have to make sure your users did not use one of your controllers' names as a landing page word, adding complexity to your program. 在不限制子域的情况下,您必须确保您的用户没有使用您的控制器名称之一作为登录页面单词,从而增加了程序的复杂性。
There is a plugin, called subdomain-fu, which should allow you to restrict your routes to a subdomain. 有一个名为subdomain-fu的插件,它允许您将路由限制为子域。 The project is located at https://github.com/mbleigh/subdomain-fu . 该项目位于https://github.com/mbleigh/subdomain-fu 。 I hope this helps, if not let me know. 如果不让我知道,我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.