这就是我some arbitrary outer text
<textarea as="codebox">
<!-- BEGIN switch_user_authreply -->
<div id="mobileActionBar">
<a href="{U_POST_REPLY_TOPIC}" rel="nofollow" class="navbar">
<div class="mobileActionLabel">{L_POST_REPLY_TOPIC}</div>
</a>
</div>
<!-- END switch_user_authreply -->
</textarea>
some arbitrary outer text
模型的样子。
Link
使用SecureRandom生成id: 6,
long_url: "http://google.com",
short_url: "91befc",
created_at: Sun, 22 Nov 2015 11:53:01 UTC +00:00,
updated_at: Sun, 22 Nov 2015 11:53:01 UTC +00:00>
,以努力创建类似bit.ly的过程。
如果有人复制并粘贴http://bitlyclone.com/3jdjh,这是他们“缩短”链接时显示的内容,我希望它在数据库中查找short_url
并重定向到它。 / p>
答案 0 :(得分:1)
你可以这样做:
# app/controllers/links_controller.rb
def show
link = Link.find_by!(short_url: params[:slug])
redirect_to link.long_url
end
这假定您确保long_url
在添加时有效。此外,如果输入的find_by!
不存在,则使用short_url
可确保呈现404。
# config/routes.rb
get '/:slug', to: 'links#show'
您的路线将直接指向此控制器操作,并将short_url
作为参数传递给它。
routes.rb
文件中)匹配,因此请确保它不会优先于您应用中的其他路线。