2014-11-19 4 views
1

URL 단축키 역할을하는 레일 앱을 만들려고합니다. 경로 구성에 문제가 있습니다. 사용자가 내 사이트를 방문하여 사이트로 리디렉션되도록하려면 어떻게해야합니까? 그들이 입력 한 URL에 기본. IE의 mysite.com/any_random_url.URL 단축키의 경로 설정

routes.rb

Rails.application.routes.draw do 
    get 'home/index' 
    get 'home/about' 
    get 'home/:id' => 'home#show'  

    root 'home#show/:id' 
    .. 

여러 가질 수 있도록하려면 home_controller.rb

class HomeController < ApplicationController 
    def index 
    end 

    def about 
    end 

    def show 
    url = params[:id] 
    @url = ShortUrl.where(["url = ?", url]).first 
    if @url.nil? 
     return redirect_to action: 'index', status: 307 
    else 
     return redirect_to @url 
    end 
    end 

답변

2

은 '당신에게 슬래시 뭔가있을거야. g like :

get '*id', to: 'home#show' 

get ':id', to: 'home#show' 

당신은 Rails Guide

에서 더 많은 정보를 찾을 수 있습니다 :/23af1)가 사용하는 아마 더 낫다