변수 네임 스페이스를 사용할 수 있습니까? 나는 다음과 같은 편안한 자원을 가지고 :Rails 3 변수 네임 스페이스를 사용하는 라우팅 리소스
resources :articles
resources :persons
그러나 나는 그것이 형태의 URL로 응답하도록 변수 네임 스페이스 내부의 범위이 필요 : 예를 들어
':edition/:controller/:action/:id'
:
/foobar/article/edit/123
또는 /bazbam/person/edit/345
입니다. resources
방법으로이 작업을 수행 할 수 있습니까? 아니면 수공예로해야합니까? 미리 :edition
에 가능한 값을 알 수 없습니다. 이것들은 내 ApplicationController
에 before_filter
에 올려다 보입니다.
내가 할 일은 이것 뿐이니?
scope ':edition' do
resources :articles
resources :matches
resources :teams
end
는 UPDATE : 위의 범위 지시어를 사용하는 경우, 내가 같은 경로 원하는 얻을 :
articles GET /:edition/articles(.:format) {:action=>"index", :controller=>"articles"}
POST /:edition/articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /:edition/articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /:edition/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /:edition/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /:edition/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /:edition/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
matches GET /:edition/matches(.:format) {:action=>"index", :controller=>"matches"}
POST /:edition/matches(.:format) {:action=>"create", :controller=>"matches"}
new_match GET /:edition/matches/new(.:format) {:action=>"new", :controller=>"matches"}
edit_match GET /:edition/matches/:id/edit(.:format) {:action=>"edit", :controller=>"matches"}
match GET /:edition/matches/:id(.:format) {:action=>"show", :controller=>"matches"}
PUT /:edition/matches/:id(.:format) {:action=>"update", :controller=>"matches"}
DELETE /:edition/matches/:id(.:format) {:action=>"destroy", :controller=>"matches"}
teams GET /:edition/teams(.:format) {:action=>"index", :controller=>"teams"}
POST /:edition/teams(.:format) {:action=>"create", :controller=>"teams"}
new_team GET /:edition/teams/new(.:format) {:action=>"new", :controller=>"teams"}
edit_team GET /:edition/teams/:id/edit(.:format) {:action=>"edit", :controller=>"teams"}
team GET /:edition/teams/:id(.:format) {:action=>"show", :controller=>"teams"}
PUT /:edition/teams/:id(.:format) {:action=>"update", :controller=>"teams"}
DELETE /:edition/teams/:id(.:format) {:action=>"destroy", :controller=>"teams"}
지금에 :edition
을 참조 할 수있어 내 ApplicationController
:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
before_filter :get_edition
def get_edition
@edition = Edition.first(:conditions => { :FriendlyName => params[:edition] })
end
end
이제이 작업을 수행하는 가장 좋은 방법인지 확인하고 싶습니다.
하지만 params [: edition]은 무엇으로 해결 되나요? 그것은 심각하게 foobar 또는 bazbam인가? 그렇다면 승리! 나는 뭔가를 배웠다! – Cory
'scope ': edition''이 실제로 작동합니다! 그러나 link help ("Article", @article)','form_for (@article)','redirect_to (@article)'와 같은 URL helpers에서 많은 문제가 발생하면 에러가 발생합니다. 나는'scope'의 디자인 목적이 정적 네임 스페이스를위한 것이라고 생각한다. 왜 당신은 단지 param으로서 url의 끝 부분에 에디션을 추가하지 않습니까? 레일을 따라 가면 시간을 절약 할 수 있습니다. – Kevin
@Kevin - URL을 RESTful하게 유지하고 싶습니다. 제 경우에는 각 리소스가 에디션과 관련하여 참으로 독특합니다. 따라서 REST의 의도를 모순하지 않습니다. param으로 전달할 수는 있지만 URL을 가능한 한 인도적으로 유지하고 싶습니다. 또 다른 옵션은 사용자가 application.html.erb의 드롭 다운을 통해 버전을 전환하고 해당 값을 쿠키에 저장할 수있게하는 것입니다. –