I try to list all doctors in my project using <%= link_to 'Registration to visit', doctors_index51_path %> but I get error: Couldn't find Doctor with id=index51
I have added get "doctors/index51" to routes and I have something like this in 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
Help me please, I am new in Ruby and I'm fighting with my project to school :)
In rake routes output I have:
index51_doctors GET /doctors/index51 (.:format) doctors#index51
Error:
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"}
probably this is what you need.
resources :doctors do
collection do
get 'index51'
end
end
edit: (after routes.rb was posted)
comment 4th statement
#resources :doctors
Its defined twice, isn't it?
This is the easiest way to achieve what your want!
get "doctors/index51", to: "doctors#index51"
and what you have done is usually works in static pages!
Hope to help!
Just looked at your routes file and noticed you are using routes like get "patients/select51"
and get "patients/show51"
What these routes do? select patient with id 51 and show patient with id 51? You are not making rails RESTful routes. You should look into rails documentation to get a better idea of what exactly is going on
Error: Couldn't find Doctor with id=index51 states that rails is looking at index51 as an id.
Fix:
You have resources :doctors inside your routes.rb file which creates seven different routes in your application, all mapping to the doctors controller like:
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
and below that you have:
resources :doctors do
collection do
get 'index51'
end
end
which is creating a get /doctors/index51 doctors#index51 and conflicting so you need to remove the earlier one. Although i'll still suggest sticking with the defaults but that will require you to make an action index rather than index51
Related
I'm having issues setting up devise in a fresh rails app. I've just created a fresh Rails app, added the devise gem and ran bundle install. Now when I try to run rails generate devise:install I get this error:
/Users/Bobo/.rvm/gems/ruby-2.2.4/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:557:in `add_route': Invalid route name, already in use: 'new_user_session'
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming.
I'm confused, I haven't even touched my routes file yet, when I run rake:routes it says: You don't have any routes defined!
Why am I getting this error? It is literally the first step I took..
Edit: my routes file
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
You don't have any routes defined, they are all commented out. Uncomment the root route e.g. change
# root 'welcome#index'
root 'welcome#index'
I did a fresh install in a new directory, no more problems.
Change your routes. As mentioned above set the root
Make sure you have a controller called WELCOME with template and action 'index'
Then root => welcome#index will point to that action.
Run
rails generate devise:install
Run
rails g devise Model
This will automatically add routes for your Model to the routes.rb file
Run rake db:migrate OR with rails5 rails db:migrate
Than you have create a Model with Devise.
Rails.root: /home/chris/rails_projects/app1
I get the message about routes. my routes.rb files show:
I do not know how to approach this at all. I am using Ubuntu 12.04 in Virtualbox.
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
You need to create a controller and a view to direct your app somewhere.
Then you need to route your app to that action by editing your routes file. For example you can uncomment the line:
root 'welcome#index'
This will then mean that when you navigate to the root of your site at the root directory (/app1) will go to the welcome controller, to its index action.
To learn more, have a look at: http://guides.rubyonrails.org/routing.html#using-root
Routes
You need to define the routes your application will use, as follows:
#config/routes.rb
root 'application#index'
This will direct any of the "homepage" traffic to the following controller/action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
--
Routing in Rails
You have to remember that Rails is an MVC-centric web application framework
This means every time you wish to access a "url", you need to define the "route" in your routes.rb file. The way to do this is to reference the relevant controller#actions in your routes file - using the resources method:
#config/routes.rb
root "application#index"
resources :controller_1, :controller_2
As Rails is object-orientated, the routes of your application will generally have to reflect the various resources it has inside. Most people try and define routes based on the presumed "flow" of the app - the truth is that you want to construct them around objects, which typically means controller and/or models
I'm applying a design for a Rails app, I usually works on PHP, anyway, I'm doing the reset password modifications the problem is I have the views for reset password, but not the email.
I know there is a generate devise:views which pull up even the email, the problem when I do this is this "damage" the current customizations in some part already done in this app by other programmer.
How can I pull up just the reset instructions email view on devise, so I can edit it?
Thanks.. I guess you can be more specific when you generate views of devise using
generate devise:views
Even more specific than
generate devise:views users
Also in the email the link is directed to localhost, for this issue I found this Devise: edit_password_url in password reset email is sending users to url/api/v1/
The problem is I can't make that work in my routes.rb which is here
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
scope "/admin" do
resources :users, :controller => 'admin/users'
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
match '*a', :to => 'errors#routing'
# 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 :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# 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 => "devise/sessions#new"
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
# 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
How in the name of rails can I make this work so that I can finish this?
Rails has some helpers that can come in useful when you do something wrong, such as
rails generate model Fish
rails g controller Fishes
You can also apply this to destroying the action task if you need too
rails destroy model Fish
rails destroy controller Fishes
You can apply this to anything with rails at the front so:
rails generate devise:views
rails destroy devise:views
I followed each and every single step for devise to work on my localhost as discussed under this tutorial - http://www.slideshare.net/wleeper/devise-and-rails upto page 8 of 22 and I was expecting the same user login form as mentioned on the page 8 but apart from this, i am getting this error-
If i keep the comments as it is on routes.rb file,
# match ':controller(/:action(/:id))(.:format)'
then this message flags -
And when i uncomment this line it show this error-
Have very less idea about rails, as I am from PHP background, but still trying some practical tests before theory, so let me know what exactly im doing wrong.
My routes.rb file -
Prjmgt::Application.routes.draw do
devise_for :users
# 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 :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# 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'
root :to => 'home#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
Just run a rake routes command and find this -
You are accessing the wrong url.. you should try it with
http://application/users/login
you dont have the right root url like
root :to => redirect("/users/login")
I am a newbie to the ruby on rails platform and I was just trying out couple of example codes.
I was trying to run this example http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/
I followed all the instructions but I am getting this error
ActionController::RoutingError (uninitialized constant CsvImportController):
Please help me to bash this error.
Here is my Routes.rb
Imports::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
get "csv_imports/csv_view"
#map.resources :imports
#map.import_proc '/import/proc/:id', :controller => "imports", :action => "proc_csv"
# 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)
resources :imports
import_proc '/import/proc/:id', :controller => "csv_imports", :action => "pro_csv"
end
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# 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 => 'csv_imports#csv_view'
# 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)))'
Your file, containing the class CsvImportsController should be named csv_imports_controller. I believe that is the error. In your routing, you should have
resources :csv_imports
[EDIT] On the other hand, if your controller is named ImportsController, placed in imports_controller.rb, then inside your routing, you should have
resources :imports
Rails automatically tries to tie things together based on the names. This is what makes things easy if you follow them correctly. So resources :imports will assume there is a controller called ImportsController, which can be found in app/controllers/imports_controller.rb. It is best practice to call the relevant model Import, to be found in app/models/import.rb.
Hope this helps.
(also note that the blog-post you mention is for Rails 2 and not Rails 3)