2016-11-08 58 views
0

我用用戶和帖子創建了一個項目。我在用戶個人資料上有帖子。要聲明@posts我創建了UserPosts控制器,並把這個單軌導軌

def index 
    @posts = current_user.posts 
    end 

現在我需要編寫路由到這個控制器。我做了:

get 'users/profile', as: 'user_root', controllers: { profile: "userposts" } 

並且得到了nilNil錯誤。我應該如何根據CRUD正確執行此操作?

回答

2

你不要說這個方法應該採取行動「指數」。

試試這樣說:

get "user_posts/index" => "user_posts#index", :as => 'user_root' 

第一個字符串是你將不得不插入到達此行的鏈接,二是控制器和行動,以#分隔。控制器在這裏'UserPosts',行動'索引'。

或者,如果你想通過你的user_profile作爲PARAM和有另一個鏈接:

get "user_root/:profile" => "user_posts#index" 

還有對軌道路由具有很好的指導: http://guides.rubyonrails.org/routing.html