2014-01-29 3 views
0

'/'다음의 모든 문자열이 매개 변수가되는 경로를 갖고 싶습니다.경로에서 매개 변수로 전체 경로 사용

URL은 예를 들어, localhost:3000/posts/1/editparams[:path] 내가이

resource :item, path: '/:path', only: [:show, :update, :create, :destroy], constraints: { path: /.+/, format: :json } 

그런 짓을하려고했습니다

'게시물/1/편집'에 그러나이 경우 동일해야하는 경우 I 끝에 .json이 있으면 경로 매개 변수에도 포함됩니다. 난 제약 조건 /.+\./에서 다른 정규식을 시도했지만 어느 것도 작동하지 않습니다.

내가 뭘 잘못하고 있니? 감사합니다 adnvance! 컨트롤러에서

+0

params[:path]의 그것을 얻을? 컨트롤러에서 이미'request.env [ "PATH_INFO"]'를 실행하여 같은 결과를 얻을 수 있습니다 (쉽게 제거 할 수있는 시작 슬래시가 있음). – MrDanA

+0

@Pavel "Yo dawg", 당신은 regexing : path라고 들었습니다. 그래서 path : _path로 바꿀 수 있습니다 : regexing하는 동안 경로 : _path – SoAwesomeMan

답변

0

, 왜 다른 PARAM이 필요합니까 env['PATH_INFO'] 대신

0
# SoAwesomeMan 
# Rails 3.2.13 
Awesome::Application.routes.draw do 
    resources :items, path: ':_path', _path: /[^\.]+/ 
    # http://localhost:3000/posts/1/edit.json?q=awesome 
    # => {"q"=>"awesome", "action"=>"index", "controller"=>"items", "_path"=>"posts/1/edit", "format"=>"json"} 
end 

class ItemsController < ApplicationController 
    before_filter :defaults 
    def defaults 
    case request.method 
    when 'GET' 
     case params[:_path] 
     when /new\/?$/i then new 
     when /edit\/?$/i then edit 
     when /^[^\/]+\/[^\/]+\/?$/ then show 
     else; index 
     end 
    when 'POST' then create 
    when 'PUT' then update 
    when 'DELETE' then destroy 
    else; raise(params.inspect) 
    end 
    end 

    def index 
    raise 'index' 
    end 

    def show 
    raise 'show' 
    end 

    def new 
    raise 'new' 
    end 

    def edit 
    raise 'edit' 
    end 

    def create 
    raise 'create' 
    end 

    def update 
    raise 'update' 
    end 

    def destroy 
    raise 'destroy' 
    end 
end