2010-02-09 42 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