I am trying to figure out changing this routes.rb to the new rails 3 syntax but it's proving to be quite difficult... I know most of the sutff is simply removing the map.
but some of these routes I cant figure out what they were supposed to do in rails 2 to begin with... so if someone could help me get this to work without deprecation warnings I'd appreciate it'. Please Help.
MyProject::Application.routes.draw do |map|
map.resources :grading_levels
map.resources :class_timings
map.resources :subjects
map.resources :attendances
map.resources :employee_attendances
map.resources :attendance_reports
map.feed 'courses/manage_course', :controller => 'courses' ,:action=>'manage_course'
map.feed 'courses/manage_batches', :controller => 'courses' ,:action=>'manage_batches'
map.resources :courses, :has_many => :batches
map.resources :batches do |batch|
batch.resources :exam_groups
batch.resources :additional_exam_groups
batch.resources :elective_groups, :as => :electives
end
map.resources :exam_groups do |exam_group|
exam_group.resources :exams, :member => { :save_scores => :post }
end
map.resources :additional_exam_groups do |additional_exam_group|
additional_exam_group.resources :additional_exams , :member => { :save_additional_scores => :post }
end
map.root :controller => 'users', :action => 'login'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id/:id2'
map.connect ':controller/:action/:id.:format'
end
resources :grading_levels
resources :class_timings
resources :subjects
resources :attendances
resources :employee_attendances
resources :attendance_reports
match 'courses/manage_course' => 'courses#manage_course', :as => :feed
match 'courses/manage_batches' => 'courses#manage_batches', :as => :feed
resources :courses
resources :batches do
resources :exam_groups
resources :additional_exam_groups
resources :elective_groups
end
resources :exam_groups do
resources :exams do
member do
post :save_scores
end
end
end
resources :additional_exam_groups do
resources :additional_exams do
member do
post :save_additional_scores
end
end
end
match '/' => 'users#login'
match '/:controller(/:action(/:id))'
match ':controller/:action/:id/:id2' => '#index'
Related
I'm using: Rails 3.1.3, HAML and Passenger
I'm having a route problem in my staging branch, but in the other branch that was merged into staging I have no problem at all. I've tried comparing my routes files and the form files from these two branches using git diff but the two files are the same in the branches.
A weird thing is the rendered error page. Here's a link to an screenshot
Error message
And also the error from passenger:
Rendered time_entries/_form_fields.haml (173.2ms)
Rendered time_entries/_form.haml (194.7ms)
Rendered time_entries/edit.haml within layouts/application (196.6ms)
Completed 500 Internal Server Error in 448ms
ActionView::Template::Error (No route matches {:controller=>"time_entries"}):
5: .field
6: = form.submit "Save"
7: |
8: = link_to "Cancel", project_time_entries_path(#project)
app/views/time_entries/_form.haml:8:in `block in _app_views_time_entries__form_haml__894171410_105032920'
app/views/time_entries/_form.haml:3:in `_app_views_time_entries__form_haml__894171410_105032920'
app/views/time_entries/edit.haml:2:in `_app_views_time_entries_edit_haml__805073897_105087770'
I attach you the code from my route and my form
form.haml
.app-form
= error_messages_for :time_entry
= form_for [#project, #time_entry] do |form|
= render :partial => 'form_fields', :locals => {:form => form}
.field
= form.submit "Save"
|
= link_to "Cancel", project_time_entries_path(#project)
routes.rb
Titi::Application.routes.draw do
root :to => 'time_entries#index'
match 'home' => 'home#index', :as => :home
match '/api/*other' => TitiAPI
resources :projects do
member do
get 'archive'
get 'unarchive'
end
resources :viewers
resources :labels
resources :time_entries do
collection do
get 'start'
end
member do
get 'new_note'
put 'add_note'
end
end
end
resources :bookings
resources :technical_orientations, except: [:delete]
match 'merge_labels/:projects_id' => 'labels#merge_labels', :as => :merge_labels
match 'move_labels/:projects_id' => 'labels#move_labels', :as => :move_labels
resources :time_entries do
collection do
get 'start'
end
member do
get 'new_note'
put 'add_note'
end
end
resources :users do
member do
get 'time_entries'
get 'edit_password'
put 'update_password'
get 'suspend'
get 'unsuspend'
end
new do
get 'invite_form'
post 'invite'
end
collection do
get 'all'
end
end
resources :user_sessions
match 'reports/timesheet' => 'reports#timesheet', :as => :timesheet
match 'reports/:week/timesheet' => 'reports#timesheet', :as => :timesheet_week
match 'reports/timesheet_detail' => 'reports#timesheet_detail', :as => :timesheet_detail
match 'reports/dashboard' => 'reports#dashboard', :as => :dashboard_report
match 'reports/timeentries_irregulars' => 'time_entries_irregulars#timeentries_irregulars', :as => :timeentries_irregulars
match 'reports/:week/timeirregulars' => 'time_entries_irregulars#timeentries_irregulars', :as => :timeirregulars_week
match 'reports/timesheet/admin' => 'reports#timesheet_admin', :as => :timesheet_admin
match 'reports/:week/timesheet/admin' => 'reports#timesheet_admin', :as => :timesheet_admin_week
match 'reports/time_entries' => 'time_entries_reports#landing_report'
match 'reports/time_entries/make' => 'time_entries_reports#index'
match 'reports/time_entries/csv' => 'time_entries_reports#make_csv'
match 'reports/:token' => 'reports#index', :as => :reports
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#register', :as => :register
resources :companies do
collection do
get 'settings'
get "remove_photo"
end
end
match 'labels/for_project_id/:id' => 'labels#for_project_id', :as => :for_project_id
match 'holidays_for_company/:token' => 'holidays#public', :as => :public_holiday_for_company
match 'holidays_by_year' => 'holidays#by_year', :as => :holidays_by_year
resources :holidays
resources :clients
resources :invoices do
member do
get 'pdf_invoice'
get 'cancel'
end
resources :invoice_lines do
collection do
get 'add'
end
end
resources :payments
end
resources :tweets do
resource :tweet_likes, :path => 'likes' do
collection do
get 'names'
end
end
end
match 'select_invoices' => 'invoices#select_invoices', :as => :select_invoices
match 'pusher/auth' => 'pusher#auth'
resources :requests, :only=>[:create]
match '*request', :controller => 'requests', :action => 'options', :constraints => {:method => 'OPTIONS', :format => 'json'}
end
Thank's in advance.
On app/views/time_entries/_form.haml:8, does #project exist? Perhaps your staging branch uses a different database and hence the #project you think is there is not?
Also, run rake routes and verify the project_time_entries is in the output. Perhaps you need a :as on one of your routes.
I have routes listed as follows
resources :jobs do
resources :invoices, :only => [:show] do
get 'submit_invoice', :on => :member
end
end
So the middle route creates a url like /jobs/:job_id/invoices/:id/submit_invoice which is exactly what I want. However rails assigns the name submit_invoice_job_invoice to the path which is ugly and horrible to type.
How can I make the name just submit_invoice so that I can have submit_invoice_path and submit_invoice_url?
The answer should be:
get "/jobs/:job_id/invoices/:id/submit_invoice" => "invoices#submit_invoice",
:as => "submit_invoice"
resources :jobs do
resources :invoices, :only => [:show] do
get 'submit_invoice', :on => :member, :as => 'submit_invoice'
end
end
Use :as => 'routename' and invoke it as routename_path.
:)
In rails2, I was able to have code like this:
link_to(user.company.name, user.company)
which would map to:
/companies/id
but in rails 3, this same line of code throws a error stating:
undefined method `user_companies_path'
The obvious fix is to do something like:
link_to(user.company.name, company_path(user.company))
But I was wondering if anyone could explain the reason behind the change? The logic seemed a lot cleaner.
EDIT: Adding samples of my routes
In rails2, my routes looked like:
map.resources :users, :except => :edit, :member => { :details => :get }
map.resources :companies, :except => :edit, :member => { :details => :get }
In rails3, my routes are:
resources :users, :except => :edit do
member do
get :details
end
end
resources :companies, :except => :edit do
member do
get :details
end
end
The short answer is that the Rails 3 routing API bases your application on resources which is why these RESTful routes are being used, and also means that it does things like support constraints.
In Rails 2, you'd do:
resources :cars do
resource :models
member do
post :year
end
collection do
get :details
end
end
In Rails 3, you'd do:
map.resources :cars, :member => {:year => :post}, :collection => {:details => :get} do |cars|
cars.resource :model
end
You also have the :as key available which means you can then use named route helpers anywhere that url_for is available (i.e. controllers, mailers etc.)
Looking for a little helping understanding how this route would work?
New route:
resources :artists do
resources :users
end
entire routes
resources :artists do
resources :users
end
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :admins
match '/admin' => 'RailsAdmin/Main#index'
devise_for :users, :controllers => {:registrations => 'registrations'} do
match '/users/change_password', :to => 'registrations#change_password'
match '/users/edit_account', :to => 'registrations#edit_account'
end
resources :posts do
member do
get :likers
end
collection do
get :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
match '/a_json/:id', :to => 'artists#index'
match '/s_json/:id', :to => 'stores#index'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
end
# 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)))'
match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user
root :to => "pages#home"
end
This would create nested routes, allowing you to use URLs like /artists/5/users/45, which would call UsersController#show with a parameter artist_id which was 5, and a parameter id which was 45. All of the other usual RESTful routes are also created "nested" under a single artist.
Rails actually has a tool for showing you what routes have been generated: just run rake routes to take a peek.
how to convert this routes which is in rails 2 to rails 3
%w(a).each do |attr|
map.resources :b, :name_prefix => "#{attr}_", :path_prefix => "/#{attr.pluralize}/:#{attr}_id"
end
map.resources :a do |s|
s.resources :t do |ts|
ts.resources :p
ts.resource :m, :controller => :m
end
end
end
any idea on this?
Have you tried this?
%w(a).each do |attr|
resources :b, :name_prefix => "#{attr}_", :path_prefix => "/#{attr.pluralize}/:#{attr}_id"
end
resources :a do
resources :t do
resources :p
resources :m, :controller => :m
end
end
EDIT : as per comment
map.resources :q, :name_prefix => 'all_', :collection => { :search => :get }
can be written as
resources :q, :name_prefix => 'all_' do
collection do
get :search
end
end
Something seems off in your pasted code with indentation and an extra end?