我有一个叫 Page_controller 的控制器,另一个叫 Categories_controller 。现在,每当调用 Page_controller 中的 home 方法时,我都将被重定向到a´categories控制器,因为从那时起我想控制Category视图。这是我写的:
class PagesController < ApplicationController
def home
@categories = Category.all
redirect_to category_url(@category)
end
end
但是当我打开服务器时,我收到一条错误消息:
undefined method `category_url' for #<PagesController:0x000000000b24ce20>
该行:
redirect_to category_url(@category)
有人知道我如何将控制权从一个控制器传递给另一个控制器吗?我真的很绝望,感谢您能提供的任何帮助
编辑:这是我的route.rb文件:
Rails.application.routes.draw do
resources :comments
#resources :categories
root to: 'pages#home'
get 'index', to: 'controller_category#index'
devise_for :users
end
从控制台打印出的路线:
C:\Users\andri\Desktop\proj>bundle exec rake routes
Prefix Verb URI Pattern Controller#Action
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root GET / pages#home
index GET /index(.:format) controller_category#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
答案 0 :(得分:3)
取消注释routes.rb文件中的这一行:
#resources :categories
这将为类别定义一组路由,这将为您提供诸如category_url(category)
之类的方法。
您还将需要一个CategoriesController,否则您的下一个问题将是重定向会产生错误(没有此类控制器)。并且您需要实现CategoriesController#show
。
请参见https://guides.rubyonrails.org/getting_started.html 和https://guides.rubyonrails.org/routing.html