Routing in rails3 - ruby-on-rails-3

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?

Related

Rails 3 Routing Error in One Branch but not in the Other

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.

link_to different behavior from rails2 to rails3

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.)

restful way of routes?

Is there a better way to write such routs in rails 3 ?
I converted my application from rails 2 to rails 3 .
match "/resume/education/edit_education",
:controller => "resume/education#edit_education",
:as=>"resume_edit_education"
match "/resume/education/update_education",
:controller => "resume/education#update_education",
:as=>"resume_update_education"
match "/resume/education/cancel_education_add",
:controller => "resume/education#cancel_education_add",
:as=>"resume_cancel_education_add"
match "/resume/education/cancel_education_edit",
:controller => "resume/education#cancel_education_edit",
:as=>"resume_cancel_education_edit"
match "/resume/education/remove_education",
:controller => "resume/education#remove_education",
:as=>"resume_remove_education"
match "/resume/education/update_education_title",
:controller => "resume/education#update_education_title",
:as=>"resume_update_education_title"
match "/resume/education/move_up",
:controller => "resume/education#move_up",
:as=>"resume_education_move_up"
match "/resume/education/move_down",
:controller => "resume/education#move_down",
:as=>"resume_education_move_down"
match "/resume/education/remove",
:controller => "resume/education#remove",
:as=>"resume_remove_education"
I think you should refactor your controller like this
class Resume::EducationController
def cancel_add
end
def cancel_edit
end
def update_title
end
def move_up
end
def move_down
end
def update
end
def destroy
end
end
Then you could organize your routes thus
namespace :resume do
resource :education, :only => [:update, :destroy] do
collection do
get 'cancel_add'
get 'cancel_update'
get 'update_title'
get 'move_up' # get -> put ?
get 'move_down'
end
end
end
Look to guide Rails Routing from the Outside In

Will paginate in rails3

<%= will_paginate #semails, :renderer => 'RemoteLinkRenderer' , :remote => {
:loading => 'loadingPanel.show()',:complete => 'loadingPanel.hide()'} %>
in rails2
how to convert this to rails3
This is the routes using for rails 2 for semails
map.resources :users,
:collection => {:uapload_avatars => :post, :aselect_friend => :get, :alist_friend => :get, :aist_moderator => :get},
:member => {:anew_avatars => :get, :acreate_avatar => :post
} do |user|
user.resources :semails, :collection => { :sort => :get, :asave_draft => :post }
end
how to convert this routes in rails 3 ?
I'm facing this error
will clicking the pagination link it just redirecting to the home page is that routes issue or will paginate issue in rails 3
please help me to solve this issue
I'm not 100% about the spelling but the new routes for rails would approximate something like this:
resources :user do
collection do
post "uapload_avatars"
get "aselect_friend"
end
resources :semails do
collection do
get "sort"
post "asave_draft"
end
end
end
http://guides.rubyonrails.org/routing.html

Help migrating routes to rails 3 format

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'