RoR路由问题。调用自定义操作,但被重定向到显示操作

发布于 2024-08-20 13:23:26 字数 2065 浏览 4 评论 0原文

我正在用 ruby​​ on Rails 开发一个项目,但我遇到了一个基本问题,非常困难。我试图在我的一个控制器中调用自定义操作,但请求以某种方式被重定向到默认的“显示”操作,我无法弄清楚为什么。

edit.html.erb 中的链接:

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

development.log 中的错误:

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:

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

create_or_add_food_item_from_text.html.erb 只是有一个 div 来显示其中包含文本框的表单。我的应用程序的其余部分工作正常,但这让我很困惑。

任何帮助表示赞赏。

I am working on a project in ruby on rails and I am having a very difficult time with a basic problem. I am trying to call a custom action in one of my controllers, but the request is somehow getting redirected to the default 'show' action and I cannot figure out why.

link in edit.html.erb:

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

Error from development.log:

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 file:

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:

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

create_or_add_food_item_from_text.html.erb simply has a div to show a form with a text box in it. I have the rest of my app working fine, but this is stumping me.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-08-27 13:23:26

尝试在 :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 code> 实际上阻止了某些路径的路由。

即使不管这个问题如何,最好的做法是通过资源或命名/未命名路由显式映射所有路径,并最终消除通用 :controller/:action:controller/:action /:id 来自您的应用程序的路由。

Try adding the route to your file explicitly, before the :menus resources:

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

map.resources ...

Routes declared earlier have higher priority, and the problem here is that map.resources actually prevents certain paths from being routed.

Even regardless of this issue, it's good practice to map all paths explicitly, either through resources or named/unnamed routes, and ultimately eliminate the generic :controller/:action and :controller/:action/:id routes from your app.

黑凤梨 2024-08-27 13:23:26

link_to 期望您的操作的路径作为第二个参数 - 看起来您传递了 link_to 错误的路径值。检查开发日志以了解 Rails 认为您正在寻找的路径。

link_to expects the path to your action as the second parameter - it looks like you are passing link_to the wrong path value. Check the development log to see what path rails thinks you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文