Ruby无法路由到站点

时间:2014-06-16 09:16:21

标签: ruby-on-rails ruby

我尝试使用<%= link_to 'Registration to visit', doctors_index51_path %>列出项目中的所有医生,但收到错误:Couldn't find Doctor with id=index51 我已经将“doctor / index51”添加到路由中,我在doctors_controller中有类似的内容:

  def index51
    @doctors = Doctor.all

    respond_to do |format|
      format.html # index51.html.erb
      format.json { render json: @doctors }
    end
  end 

routes.rb中:

ZOZ::Application.routes.draw do

  resources :refferals


  resources :clinics


  resources :doctors

  get "welcome/index2"

  get "welcome/index"

  get "patients/select51"

  get "patients/show51"

  get "refferals/new"


  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  resources :patients

  resources :doctors do
    collection do
      get 'index51'
    end
  end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

请帮助我,我是Ruby新手,我正在与我的学校项目抗争:)

在rake路线输出中,我有: index51_doctors GET / doctors / index51(。:format)医生#index51

错误:

ActiveRecord::RecordNotFound in DoctorsController#show

Couldn't find Doctor with id=index51
Rails.root: D:/Studia/Bazy Danych/Projekt/Implementacja/ZOZ

Application Trace | Framework Trace | Full Trace
app/controllers/doctors_controller.rb:25:in `show'
Request

Parameters:

{"id"=>"index51"}

4 个答案:

答案 0 :(得分:3)

可能这就是你需要的。

 resources :doctors do
    collection do
      get 'index51'
    end
  end

编辑:(在routes.rb发布后) 评论第4条声明

#resources :doctors

它定义了两次,不是吗?

答案 1 :(得分:0)

routes文件中,您需要确保index51操作已定义

  • 使用正确的动词(我猜是GET)
  • 作为收集行动

喜欢这里:

collection :doctors do
  collection do
    get :index51
  end
  # or
  get :index51, on: :collection
end

答案 2 :(得分:0)

这是达到你想要的最简单方法!

get "doctors/index51", to: "doctors#index51"

你所做的事情通常在静态页面中起作用!

希望能帮到你!

答案 3 :(得分:0)

只是查看了你的路线文件,发现你正在使用像 get&#34; patients / select51&#34; 得到&#34;患者/ show51&#34;

这些路线做什么?选择id为51的患者并显示id为51的患者? You are not making rails RESTful routes。您应该查看rails documentation以更好地了解究竟发生了什么

Error: Couldn't find Doctor with id=index51表示rails将index51视为id。

<强>修正:

您的routes.rb文件中有resources :doctors,它会在您的应用程序中创建七条不同的路径,所有路由都映射到医生控制器,如:

GET /doctors    doctors#index   display a list of all doctors
GET /doctors/new    doctors#new return an HTML form for creating a new doctor
POST  /doctors  doctors#create  create a new doctor
GET /doctors/:id    doctors#show    display a specific doctor
GET /doctors/:id/edit   doctors#edit    return an HTML form for editing a doctor
PATCH/PUT   /doctors/:id    doctors#update  update a specific doctor
DELETE  /photos/:id doctors#destroy delete a specific doctor

以下,你有:

resources :doctors do
 collection do
  get 'index51'
 end
end

正在创建get /doctors/index51 doctors#index51并发生冲突,因此您需要删除之前的版本。虽然我仍然建议坚持使用默认值,但这需要你制作一个动作索引而不是index51