2010-12-10 3 views
13

레일스에서 ​​모델의 모델을 네임 스페이스로 모델링 할 수 있습니까? url_for에서 올바른 동작을 얻으시겠습니까?레일 url_for 및 namespaced 모델

는 예를 들어, 여기, url_for 작품은 예상대로 : 모듈에 User 모델을 가하고 후, url_for 정의되지 않은 방법 m_user_path에 대해 불평 반면

# app/models/user.rb 
class User < ActiveRecord::Base 
end 

# config/routes.rb 
resources :users 

# app/views/users/index.html.haml 
= url_for(@user) # /users/1 

:

# app/models/m/user.rb 
module M 
    class User < ActiveRecord::Base 
    end 
end 

# config/routes.rb 
resources :users 

# app/views/users/index.html.haml 
= url_for(@user) # undefined method 'm_users_path' 

그것을 할 수 있습니다 url_for 모듈을 M::User에서 무시하고 url_for(@user)의 경우 0 대신에 user_path을 반환하십시오.?

UPDATE는

그래서, 이후 거의 오년, 여기, ESAD에 감사 솔루션입니다. 이것은 Rails 4.2에서 테스트되었습니다.

# app/models/m/user.rb 
module M 
    class User < ActiveRecord::Base 
    end 
end 

# app/models/m.rb 
module M 
    def self.use_relative_model_naming? 
    true 
    end 
    def self.table_name_prefix 
    'm_' 
    end 
end 

# config/routes.rb 
resources :users 

# app/views/users/index.html.haml 
= url_for(@user) # /users/1 

참고로 bin/rails g scaffold m/user 모델 뷰 및 제어를 생성 할 때, 뷰 컨트롤러도 네임 스페이스한다. app/views/m/usersapp/views/users으로, app/controllers/m/users_controller.rbapp/controllers/users_controller.rb으로 이동해야합니다. 모델 M::User을 제외한 모든 곳의 모듈 M에 대한 참조를 제거해야합니다.

마지막으로 목표는 네임 스페이스 모델이지만보기 및 컨트롤러는 아닙니다. esads 솔루션을 사용하면 M (User 포함) 모듈이 경로에 표시되지 않도록 명시 적으로 말합니다. 따라서, 실질적으로 M은 제거되고 User 만 남습니다.

사용자 모델은 이제 app/views/models/m/user.rb에 있고 사용자 컨트롤러는 app/views/controllers/users_controller.rb에 있으며보기는 app/views/users에 있습니다.

답변

23

을 할 범위를 사용하는 모든 경로를 보려면 생성 된 경로 이름의 접두어 :

module M 
    def self.use_relative_model_naming? 
    true 
    end 
end 
+0

이것은 황금! OP가 묻고있는 것을 정확히 해결합니다. (그리고 내가 뭘 찾고 있었습니까?) 감사합니다. – Ivar

+0

여기에 +1을 더 넣을 수 있다면 !!!!! 고맙습니다! – eirc

+0

이 메소드는 Rails API에서 문서화 된 첫 번째 메소드 여야합니다! – olhor

4

사용은

namespace "blah" do 
    resources :thing 
end 

는 다음 경로는 appropiately 지정됩니다.

rake routes 

+0

감사합니다, 나는 그것을 시도했지만 문제가 해결되지 않습니다. 따라서 다른 네임 스페이스에있는 모델과 컨트롤러를 사용하도록 경로를 설정할 수 있습니까? 즉, 컨트롤러 경로를 건드리지 않고 모델 인스턴스가 주어진 경우 url_for의 동작에 영향을 미치는 방법이 있습니까? 예를 들어 모델 M :: User는 app/models/m/user.rb에 정의되지만 제어기는 app/controllers/user_controller.rb에 정의됩니다. – sebastian

+0

또한 RESTFul 리소스가 아닌 경우 어떻게합니까? 나는이 문제가 있고 내가 다루고있는 컨트롤러를 리소스에 넣을 수 없다. – Ibrahim

+0

resources : 할 일 (새 줄) "foo"=> "controller # action". – sethvargo

3

경로

resources :users, :module => "m" 

에 모듈을 지정하거나 방지하기 위해 포함 된 모듈에 use_relative_model_naming?을 정의 그것을

scope :module => "m" do 
    resources :users 
end 
+0

완벽하게 작동합니다! 감사합니다 :) – Volte

0

내 경우에는 내 application_helper.rb 파일에서 url_for 메소드를 재정 의하여 내 네임 스페이스 : mkp의 모든 경로에 네트워크 매개 변수를 추가합니다.내가 필요하지 않습니다 - 경로 (컨트롤러)가 같은 네임 스페이스를 사용하기에 적합 할 것, url_for 모델 네임 스페이스에 대한 정보를 얻는다하더라도 :

module ApplicationHelper 
    def url_for(options = {}) 
    if options.is_a?(Hash) && options.has_key?(:controller) 
     if options[:network].nil? && options[:controller].match(/^mkp\//).present? 
     options[:network] = @network.downcase 
     end 
    end 
    super(options) 
    end 
end