2010-02-09 41 views
1

我正在使用rails上的ruby項目工作,而且我遇到了一個非常困難的時間,並且遇到了一個基本問題。我試圖在我的一個控制器中調用一個自定義動作,但該請求以某種方式重定向到默認的「show」動作,我找不到原因。在edit.html.erbRoR路由問題。調用自定義動作,但獲取重定向來顯示動作

鏈接:

ActiveRecord::RecordNotFound (Couldn't find Menu with ID=create_or_add_food_item_from_text): app/controllers/menus_controller.rb:20:in `show' 

的routes.rb文件:

ActionController::Routing::Routes.draw do |map| 
map.resources :nutrition_objects 
map.resources :preference_objects 
map.resources :institutions 
map.resources :locations 
map.resources :menus 
map.resources :food_items 
map.resources :napkins 
map.resources :users 
map.resource :session, :controller => 'session' 

map.root :controller=>'pages', :action=>'index' 

map.about '/about', :controller=>'pages', :action=>'about' 
map.contact '/contact', :controller=>'pages', :action=>'contact' 
map.home '/home', :controller=>'pages', :action=>'index' 

map.user_home '/user/home', :controller=>'rater', :action=>'index' 
map.user_napkins '/user/napkins', :controller=>'rater', :action=>'view_napkins' 
map.user_preferences '/user/preferences',:controller=>'rater', :action=>'preferences' 

map.blog '/blog', :controller=>'pages', :action=>'blog' 
map.signup '/signup', :controller=>'users', :action=>'new' 
map.login '/login', :controller=>'session', :action=>'new' 
map.logout '/logout', :controller=>'session', :action=>'destroy' 

# Install the default routes as the lowest priority. 
map.connect ':controller/:action' 
map.connect ':controller/:action/:id' 
map.connect ':controller/:action/:id.:format' 
end 

Menus_controller.rb:

<%= link_to 'Mass Text Entry', :action=>"create_or_add_food_item_from_text" %> 

從development.log錯誤

class MenusController < ApplicationController 
... 
    def create_or_add_food_item_from_text 
    end 
... 
end 

create_or_add_food_item_from_text.html.erb只是有一個div來顯示帶有文本框的窗體。我的應用程序的其餘部分工作正常,但這是困擾我。

任何幫助表示讚賞。

回答

0

link_to期望您的操作路徑作爲第二個參數 - 它看起來像你正在傳遞link_to錯誤的路徑值。檢查開發日誌,看看你認爲你在找什麼路徑欄。

+0

rails文檔有link_to(name,options = {},html_options = nil),所以我很確定我正確設置了鏈接 – conorgil 2010-02-09 19:14:25

3

嘗試添加路由到您的文件中明確,以前:menus資源:

map.connect "/menus/create_or_add_food_item_from_text", 
    :controller => "menus", :action => "create_or_add_food_item_from_text" 

map.resources ... 

路由宣告早期有更高的優先級,並且這裏的問題是,實際上map.resources防止被路由某些路徑。

即使不考慮這個問題,最好是通過資源或命名/未命名的路徑顯式映射所有路徑,最終從您的應用程序中消除通用的:controller/:action:controller/:action/:id路由。

+0

我重新排列了我的routes.rb文件並在菜單之前明確聲明瞭路由:像你建議的資源。它工作得很好。感謝您的快速建議 – conorgil 2010-02-09 19:13:32