2015-02-05 53 views
1

我有兩個控制器這樣的:使用相同的諧音和兩個控制器在兩個不同的文件夾

應用程序/控制器/ collection_controller.rb:

class CollectionController < ApplicationController 
    def create 
     @collection = Collection.new(name: params[:name]) 
     @collection.save! 
     render @collection 
    end 
end 

而且繼承的類:

應用/controllers/enterprise/collection_controller.rb:

class Enterprise::CollectionController < ::CollectionController 
    def create 
     @collection = Collection.new(name: params[:name]) 
     @collection.company = Company.find(params[:company]) 
     @collection.save! 
     render @collection 
    end 
end 

我有兩個部分S:

應用程序/視圖/集合/ _collection.json.jbuilder:

json.extract! collection, :title, :description 
json.users do 
    json.partial! collection.user 
end 

應用程序/視圖/集合/ _user.json.jbuilder:

json.extract! user, :name, :surname 

的問題是:

當我加載Enterprise::CollectionController#create時,我得到missing template app/views/enterprise/collections/_collection ...

我想要Enterprise :: CollectionController使用app/view/collections/_collection.json.jbuilder而不是app/view/enterprise/collections/_collection.json.jbuilder

我試着這樣做:

render @collection, partial: 'collections/collection', but I receive: 

但我得到:

missing template for ... app/views/enterprise/users/_user ... 

我怎樣才能解決這個問題?

回答

1

後你改變呈現偏

render @collection, partial: 'collections/collection' 

你沒有得到一個錯誤collection部分。你會收到user部分錯誤。你必須要渲染用戶部分的方式改變爲

json.partial! "collections/user", user: collection.user 

更新:

你可以試試append_view_path。所以基本上你將追加到默認的搜索位置

class Enterprise::CollectionController < ::CollectionController 
    before_filter :append_view_paths 
    def append_view_paths 
     append_view_path "app/views/collections" 
    end 
end 

所以Rails會app/views/enterprise/collections, app/views/shared, app/views/collections爲了尋找

您也可以使用prepend_view_path,如果你想軌在app/views/collections第一

PS搜索:我沒有測試過這個。

+0

呃......我明白了。沒有一種方法可以將'render'配置爲使用'app/views'而不是'app/views/enterprise'作爲主路徑?因此,如果我添加更多的部分,我必須寫下'json.partial! 「collections/partial_name」,var_name:@ var'而不是簡單的'json.partial!@ var' – ProGM 2015-02-05 16:36:53

+0

請參閱我的更新 – usha 2015-02-05 16:45:07

+0

它不適合我:(實際上rails可以找到'app/views/collections/_collection',但它找不到'app/views/users/_user' ... – ProGM 2015-02-05 23:55:24

相關問題