2011-04-12 20 views
0

我有這樣的自定義操作在我的視頻控制器:如何讓我的應用程序轉到此自定義視圖?

def upvoted_songs 
    @votes = current_user.videos_votes.where("value = 1") 
    @videos = @votes.videos.page(params[:page]).per(15) 
end 

這是我的路線:

resources :videos do 
    member do 
     put 'topic_update' 
     get 'upvoted_songs' 
    end 
end 

而這個鏈接在我的視頻索引視圖:

<%= link_to "Upvoted Songs", videos_path, :action => "upvoted_songs", :class => "upvoted_songs chosen_home_option" %> 

和視圖文件稱爲videos/upvoted_songs.html.erb。

爲什麼不直接鏈接到upvoted_songs.html.erb視圖,而是停留在視頻索引視圖上?

UPDATE:

這些是我的routes.rb:

root :to => "videos#index" 
resources :video_votes 
resources :videos do 
    resources :comments 
    member do 
     put 'topic_update', :on => :member 
     get 'upvoted_songs', :on => :collection, :as => 'upvoted' 
    end 
end 
resources :comment_titles 
resources :users 
resources :profiles 
resources :genres 
resources :topics 
resources :topicables 
resource :session 

我最初得到這個錯誤:

ArgumentError 

can't use member outside resource(s) scope 

然後刷新頁面後,我得到這個錯誤:

ActionController::RoutingError in Videos#index 

Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #22 raised: 

No route matches {:action=>"show", :id=>#<Video id: 485, title: "I'm Ready For You", description: nil, embed_code: nil, thumbnail_url: nil, released: nil, user_id: 57, created_at: "2011-04-02 08:47:36", updated_at: "2011-04-09 22:42:48", video_url: "http://www.youtube.com/watch?v=wy86KNtOjVg", video_votes_count: 0, vote_sum: 3, rank_sum: 28927.724512>, :controller=>"videos"} 

22: <%= link_to video.title, video_path(video), :class => "normal" %> 
+0

只是好奇:@votes = current_user.videos_votes.where(「value = 1」)應該不是> 1? :P – corroded 2011-04-12 03:29:35

+0

不,因爲我想檢索用戶已投票的所有視頻,而在我的應用程序中,當投票的值爲1時: – 2011-04-12 03:32:12

+0

我看到了,我以爲你想獲得所有最高投票。但是沒有布爾比1更好?只是再次詢問:) – corroded 2011-04-12 03:33:52

回答

3

要看在應用程序的根目錄中的命令行上,您的應用程序中可使用哪些路線使用rake routes。您應該看到一行代表upvoted_songs

然後使用它像這樣:

<%= link_to "Upvoted Songs", upvoted_songs_video_path(video), :class => "upvoted_songs chosen_home_option" %> 

既然你有一個成員路線網址助手將採取視頻對象(或ID),並生成一個URL,看起來像:/videos/7/upvoted_songs

但是,您的代碼表明您可能正在做的事情不依賴於單個視頻對象,也不需要在URL中使用該對象。因此,您需要將該路線從成員路線更改爲集合路線。該網址最終會看起來像/videos/upvoted_songs,並且您不會將它傳遞給視頻對象。

希望這有助於:)

PART 2

刪除成員塊:

resources :videos do 
    resources :comments 
    put 'topic_update', :on => :member 
    get 'upvoted_songs', :on => :collection, :as => 'upvoted' 
end 
+0

謝謝,我該如何改變它的收集路線?這就是我想要的。 – 2011-04-12 03:33:41

+1

查看下面的答案coreyward他有它 – ctcherry 2011-04-12 03:35:21

+0

@Justin我爲你發佈了一個例子。當你忘記這些東西時,你可以仔細檢查路由指南。它非常全面。 http://guides.rubyonrails.org/routing.html – coreyward 2011-04-12 03:35:25

1

你沒有一個id。做耙路線| grep upvoted,看看你的路線應該是什麼樣子。

這大概就像upvoted_songs_video_path(video)

2

要鏈接到videos_path,這是「視頻#指數」的輔助工具。

正如ctcherry所解釋的,您當前的路線使用成員路線而不是集合。下面是更多的你在找什麼:

resources :videos do 
    put 'topic_update', :on => :member 
    get 'upvoted_songs', :on => :collection, :as => 'upvoted' 
end 

然後你就可以代替剛剛videos_path使用upvoted_videos_path

+0

謝謝。你的代碼爲我拋出這個錯誤:'ArgumentError 不能使用資源(s)作用域外的成員'這是什麼意思? – 2011-04-12 03:38:55

+0

確保你已經調用了'put'topic_update',:on =>:member',並將其傳遞給'resources'。就像ctcherry在迴應中提到的那樣,確保你沒有錯過冒號。 – coreyward 2011-04-12 03:55:49

+0

我更新了問題的詳細信息,並提供了有關我的錯誤的更多詳細信息。 – 2011-04-12 04:23:39

相關問題