Trouble Upgrading Rails 2 Routes for a Redmine Plugin - ruby-on-rails

I am trying to get a Redmine plugin designed for Rails 2 to work with Rails 3.
https://github.com/dalyons/redmine-todos-scrum-plugin
I've pretty much fixed most parts, but having no success whatsoever in getting the routes to work.
The original routes for Rails 2 are as follows:
map.resources :todos, :name_prefix => 'project_', :path_prefix => '/projects/:project_id',
:member => {:toggle_complete => :post }, :collection => {:sort => :post}
map.resources :todos, :name_prefix => 'user_', :path_prefix => '/users/:user_id', :controller => :mytodos,
:member => {:toggle_complete => :post }, :collection => {:sort => :post}
map.my_todos 'my/todos', :controller => :mytodos, :action => :index
map.connect 'projects/:project_id/todos/show/:id', :controller => "todos", :action => "show"
rake routes outputs the following:
sort_project_todos POST /projects/:project_id/todos/sort(.:format) {:controller=>"todos", :action=>"sort"}
project_todos GET /projects/:project_id/todos(.:format) {:controller=>"todos", :action=>"index"}
POST /projects/:project_id/todos(.:format) {:controller=>"todos", :action=>"create"}
new_project_todo GET /projects/:project_id/todos/new(.:format) {:controller=>"todos", :action=>"new"}
toggle_complete_project_todo POST /projects/:project_id/todos/:id/toggle_complete(.:format) {:controller=>"todos", :action=>"toggle_complete"}
edit_project_todo GET /projects/:project_id/todos/:id/edit(.:format) {:controller=>"todos", :action=>"edit"}
project_todo GET /projects/:project_id/todos/:id(.:format) {:controller=>"todos", :action=>"show"}
PUT /projects/:project_id/todos/:id(.:format) {:controller=>"todos", :action=>"update"}
DELETE /projects/:project_id/todos/:id(.:format) {:controller=>"todos", :action=>"destroy"}
sort_user_todos POST /users/:user_id/todos/sort(.:format) {:controller=>"mytodos", :action=>"sort"}
user_todos GET /users/:user_id/todos(.:format) {:controller=>"mytodos", :action=>"index"}
POST /users/:user_id/todos(.:format) {:controller=>"mytodos", :action=>"create"}
new_user_todo GET /users/:user_id/todos/new(.:format) {:controller=>"mytodos", :action=>"new"}
toggle_complete_user_todo POST /users/:user_id/todos/:id/toggle_complete(.:format) {:controller=>"mytodos", :action=>"toggle_complete"}
edit_user_todo GET /users/:user_id/todos/:id/edit(.:format) {:controller=>"mytodos", :action=>"edit"}
user_todo GET /users/:user_id/todos/:id(.:format) {:controller=>"mytodos", :action=>"show"}
PUT /users/:user_id/todos/:id(.:format) {:controller=>"mytodos", :action=>"update"}
DELETE /users/:user_id/todos/:id(.:format) {:controller=>"mytodos", :action=>"destroy"}
my_todos /my/todos {:controller=>"mytodos", :action=>"index"}
/projects/:project_id/todos/show/:id {:controller=>"todos", :action=>"show"}
The nearest I have got for Rails 3 is as follows:
scope '/projects/:project_id', :name_prefix => 'project_' do
resources :todos, :controller => 'todos' do
member do
post :toggle_complete
end
collection do
post :sort
end
end
end
scope '/users/:user_id', :name_prefix => 'user_' do
resources :todos, :controller => 'mytodos' do
member do
post :toggle_complete
end
collection do
post :sort
end
end
end
match 'my/todos' => 'mytodos#index', :as => :my_todos
match 'projects/:project_id/todos/show/:id' => 'todos#show'
rake routes outputs the following:
toggle_complete_todo POST /projects/:project_id/todos/:id/toggle_complete(.:format) todos#toggle_complete {:name_prefix=>"project_"}
sort_todos POST /projects/:project_id/todos/sort(.:format) todos#sort {:name_prefix=>"project_"}
todos GET /projects/:project_id/todos(.:format) todos#index {:name_prefix=>"project_"}
POST /projects/:project_id/todos(.:format) todos#create {:name_prefix=>"project_"}
new_todo GET /projects/:project_id/todos/new(.:format) todos#new {:name_prefix=>"project_"}
edit_todo GET /projects/:project_id/todos/:id/edit(.:format) todos#edit {:name_prefix=>"project_"}
todo GET /projects/:project_id/todos/:id(.:format) todos#show {:name_prefix=>"project_"}
PUT /projects/:project_id/todos/:id(.:format) todos#update {:name_prefix=>"project_"}
DELETE /projects/:project_id/todos/:id(.:format) todos#destroy {:name_prefix=>"project_"}
POST /users/:user_id/todos/:id/toggle_complete(.:format) mytodos#toggle_complete {:name_prefix=>"user_"}
POST /users/:user_id/todos/sort(.:format) mytodos#sort {:name_prefix=>"user_"}
GET /users/:user_id/todos(.:format) mytodos#index {:name_prefix=>"user_"}
POST /users/:user_id/todos(.:format) mytodos#create {:name_prefix=>"user_"}
GET /users/:user_id/todos/new(.:format) mytodos#new {:name_prefix=>"user_"}
GET /users/:user_id/todos/:id/edit(.:format) mytodos#edit {:name_prefix=>"user_"}
GET /users/:user_id/todos/:id(.:format) mytodos#show {:name_prefix=>"user_"}
PUT /users/:user_id/todos/:id(.:format) mytodos#update {:name_prefix=>"user_"}
DELETE /users/:user_id/todos/:id(.:format) mytodos#destroy {:name_prefix=>"user_"}
my_todos /my/todos(.:format) mytodos#index
/projects/:project_id/todos/show/:id(.:format) todos#show
I am guessing that I am not using :name_prefix correctly, resulting in duplicate paths which are then omitted.
Any help would be greatly appreciated.
EDIT
I'm not sure whether this is the best way, but the following routes are working with Rails 3:
scope '/projects/:project_id' do
resources :todos, :controller => 'todos', :as => 'project_todos' do
member do
post :toggle_complete
end
collection do
post :sort
end
end
end
scope '/users/:user_id' do
resources :todos, :controller => 'mytodos', :as => 'user_todos' do
member do
post :toggle_complete
end
collection do
post :sort
end
end
end
match 'my/todos' => 'mytodos#index', :as => :my_todos
match 'projects/:project_id/todos/show/:id' => 'todos#show'
I removed :name_prefix from the scope and added :as to resource.

Related

rails 3 route path building issue

I have such route:
scope ':property', :constraints => { :property => /inv|um|tm|pp/ } do
resources :user_templates do
member do
get 'download/:idcreated', :action => 'download'
end
end
end
In browser http://127.0.0.1:3000/inv/user_templates/6/download/12?locale=uk, it works.
But How can I build resource path?
I tried:
download_user_template_path(params[:property], #id, #idcreated)
but I got that message:
No route matches {:action=>"download", :controller=>"user_templates", :locale=>:uk, :property=>"inv", :id=>76, :format=>6}
It doesn't work too:
download_user_template_path('property' => params[:property], 'id' => #tmp_id, 'idcreated' => #created_temp_row[:id])
rake routes
GET /:property/user_templates/:id/download/:idcreated(.:format) {:property=>/inv|um|tm|pp/, :action=>"download", :controller=>"user_templates"}
user_templates GET /:property/user_templates(.:format){:property=>/inv|um|tm|pp/, :action=>"index", :controller=>"user_templates"}
POST /:property/user_templates(.:format){:property=>/inv|um|tm|pp/, :action=>"create", :controller=>"user_templates"}
new_user_template GET /:property/user_templates/new(.:format){:property=>/inv|um|tm|pp/, :action=>"new", :controller=>"user_templates"}
edit_user_template GET /:property/user_templates/:id/edit(.:format){:property=>/inv|um|tm|pp/, :action=>"edit", :controller=>"user_templates"}
user_template GET /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"show", :controller=>"user_templates"}
PUT /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"update", :controller=>"user_templates"}
DELETE /:property/user_templates/:id(.:format){:property=>/inv|um|tm|pp/, :action=>"destroy", :controller=>"user_templates"}
Any help plz?
Simply add :as => :download, and use rake routes CONTROLLER= ...

What is wrong with my routes?

root :to => "index#home"
#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get
match "/tattoo" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get
members section and its nested images
resources :members, :except => [:new, :create] do
resources :tattoos
end
Thats whats in my routes.rb file. They produce:
root /(.:format) {:controller=>"index", :action=>"home"}
submit GET /submit(.:format) {:controller=>"index", :action=>"new"}
tattoo POST /tattoo(.:format) {:controller=>"index", :action=>"create"}
GET /tattoo/:id(.:format) {:controller=>"index", :action=>"show"}
tagged GET /tagged(.:format) {:controller=>"index", :action=>"tagged"}
DELETE /tattoo/:id(.:format) {:controller=>"index", :action=>"destroy"}
tattoos GET /tattoos(.:format) {:controller=>"index", :action=>"index"}
members GET /members(.:format) {:action=>"index", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET /members/:id(.:format) {:action=>"show", :controller=>"members"}
PUT /members/:id(.:format) {:action=>"update", :controller=>"members"}
DELETE /members/:id(.:format) {:action=>"destroy", :controller=>"members"}
But i have a problem. For some reason, when I try to go to mysite.com/submit
I used to get this error
No route matches {:controller=>"images"}
on
<%= form_for #tattoo, :html =>{:multipart => true} do |f| %>
but that has magically changed to:
undefined method `images_path'
on the same line.
when my controller has this:
indexcontroller
def new
#tattoo = Image.new
end
def create
#tattoo = Image.new(params[:image])
if #tattoo.save
flash[:success] = "Tattoo sent in for approval!"
redirect_to(images_path)
else
render :action => "new"
end
end
And then this link_to:
<%= link_to "Manage tattoos", member_tattoos_path() %>
give me this error:
No route matches {:controller=>"tattoos"}
I thought I was beginning to understand routes and had a decent grasp but I dont get whats going on!
You need to pass in a member object to edit_member_path.
<%= link_to "Edit profile", edit_member_path(#member) %>
edit_member_path should know the id of the member you want to edit. Please try
<%= link_to "Edit profile", edit_member_path(#member) %>
For No route matches {:controller=>"images"}; since the action image is not defined in your route, please try to stop and restart the server and check if there is any plugin like Paperclip in place.

Rails Functional test on a custom route

I have the following routes in my app:
GET /admin/comments(.:format) {:controller=>"admin/comments", :action=>"index"}
admin_comments POST /admin/comments(.:format) {:controller=>"admin/comments", :action=>"create"}
new_admin_comment GET /admin/comments/new(.:format) {:controller=>"admin/comments", :action=>"new"}
GET /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"show"}
PUT /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"update"}
admin_comment DELETE /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"destroy"}
edit_admin_comment GET /admin/comments/:id/edit(.:format) {:controller=>"admin/comments", :action=>"edit"}
admin_approve_comment /admin/comments/approve/:id {:module=>"admin", :controller=>"admin/comments", :action=>"approve"}
admin_reject_comment /admin/comments/reject/:id {:module=>"admin", :controller=>"admin/comments", :action=>"reject"}
which is declared as:
namespace "admin" do
resources :comments
match '/comments/approve/:id' => 'comments#approve', :as => "approve_comment", :module => "admin"
match '/comments/reject/:id' => 'comments#reject', :as => "reject_comment", :module => "admin"
end
and a functional test like this:
context "a POST to :approve" do
setup do
comment = Factory(:comment)
sign_in Factory(:admin)
post :approve, :id => comment.id
end
should respond_with :success
end
However, when I run this I get:
test: a POST to :approve should respond with 200. (Admin::CommentsControllerTest):
ActionController::RoutingError: No route matches {:action=>"approve", :id=>339, :controller=>"admin/comments"}
What's wrong here? What stupid mistake am I making?
These routes look like member routes to me. So routing this way
namespace "admin" do
resources :comments do
member do
get :approve
get :reject
end
end
end
This will generate routes like /admin/comments/:id/approve . This is the rails way as far i know.
I think it's better to put match before resources. Because it's not check if it's good or not.

Nested routes in Rails with an alias

I have a primary model for my project, Place.rb with a places_controller, and I've currently got it exactly the way I want for the users end of my project. With a nested photos controller, review, etc.
I want now to create a management resource which is mostly just an alias for Places, with its own nested resources, some of them overlapping, some of them new.
I tried creating a new controller for it called Manage, but I'm having a hard time with routes. I'm not quite sure the hangup is, but I figure I'm doing something very wrong. I had little difficulty when I was using Places as controller to a real model and nesting other resources below it.
But for example trying to create a new record for a nested resource doesn't route correctly.
I can get a route path like new_manage_room_path(#place) for a link_to to work fine. But
for creating a New announcement in a form:
form_for manage_room_path(#place) doesn't work correctly given a valid id. I've tried many other combinations supplying the object and :url.
Should I avoid using a separate controller and just create an alias or what is the special routing for this purpose?
map.resources :manage, :collection => { :upcoming => [ :post, :get ], :pending => [ :post, :get ] } do |manage|
manage.resources :rooms
manage.resources :room_rates, :as => :rates
manage.resources :availables
manage.resources :manage_bookings, :as => :bookings
end
map.resources :places do |place|
place.resources :bookings
place.resources :photos, :collection => { :sort => :post }
place.resources :reviews, :only => [ :index, :show ]
end
manage_rooms GET /manage/:manage_id/rooms(.:format) {:controller=>"rooms", :action=>"index"}
POST /manage/:manage_id/rooms(.:format) {:controller=>"rooms", :action=>"create"}
new_manage_room GET /manage/:manage_id/rooms/new(.:format) {:controller=>"rooms", :action=>"new"}
edit_manage_room GET /manage/:manage_id/rooms/:id/edit(.:format) {:controller=>"rooms", :action=>"edit"}
manage_room GET /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"show"}
PUT /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"update"}
DELETE /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"destroy"}
manage_room_rates GET /manage/:manage_id/rates(.:format) {:controller=>"room_rates", :action=>"index"}
POST /manage/:manage_id/rates(.:format) {:controller=>"room_rates", :action=>"create"}
new_manage_room_rate GET /manage/:manage_id/rates/new(.:format) {:controller=>"room_rates", :action=>"new"}
edit_manage_room_rate GET /manage/:manage_id/rates/:id/edit(.:format) {:controller=>"room_rates", :action=>"edit"}
manage_room_rate GET /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"show"}
PUT /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"update"}
DELETE /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"destroy"}
manage_availables GET /manage/:manage_id/availables(.:format) {:controller=>"availables", :action=>"index"}
POST /manage/:manage_id/availables(.:format) {:controller=>"availables", :action=>"create"}
new_manage_available GET /manage/:manage_id/availables/new(.:format) {:controller=>"availables", :action=>"new"}
edit_manage_available GET /manage/:manage_id/availables/:id/edit(.:format) {:controller=>"availables", :action=>"edit"}
manage_available GET /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"show"}
PUT /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"update"}
DELETE /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"destroy"}
manage_manage_bookings GET /manage/:manage_id/bookings(.:format) {:controller=>"manage_bookings", :action=>"index"}
POST /manage/:manage_id/bookings(.:format) {:controller=>"manage_bookings", :action=>"create"}
new_manage_manage_booking GET /manage/:manage_id/bookings/new(.:format) {:controller=>"manage_bookings", :action=>"new"}
edit_manage_manage_booking GET /manage/:manage_id/bookings/:id/edit(.:format) {:controller=>"manage_bookings", :action=>"edit"}
manage_manage_booking GET /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"show"}
PUT /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"update"}
DELETE /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"destroy"}
pending_manage POST /manage/pending(.:format) {:controller=>"manage", :action=>"pending"}
GET /manage/pending(.:format) {:controller=>"manage", :action=>"pending"}
upcoming_manage POST /manage/upcoming(.:format) {:controller=>"manage", :action=>"upcoming"}
GET /manage/upcoming(.:format) {:controller=>"manage", :action=>"upcoming"}
manage_index GET /manage(.:format) {:controller=>"manage", :action=>"index"}
POST /manage(.:format) {:controller=>"manage", :action=>"create"}
new_manage GET /manage/new(.:format) {:controller=>"manage", :action=>"new"}
edit_manage GET /manage/:id/edit(.:format) {:controller=>"manage", :action=>"edit"}
manage GET /manage/:id(.:format) {:controller=>"manage", :action=>"show"}
PUT /manage/:id(.:format) {:controller=>"manage", :action=>"update"}
DELETE /manage/:id(.:format) {:controller=>"manage", :action=>"destroy"}
Try:
<% form_for #new_room, :url => manage_rooms_path(#place) do |f| %>
or maybe it will work this way:
<% form_for manage_rooms_path(#place, #new_room) do |f| %>
#new_room is new instance of Room model, so in controller add:
#new_room = Room.new

Nested routing

How do I write a route that maps a path like this?
/powerusers/bob/article-title
This is what I got so far:
map.resources :users, :as => "powerusers" do |users|
users.resources :articles, :as => ''
end
This gives me the following route:
/powerusers/:user_id//:id
How do I get rid of the double backslah? /powerusers/admin//first-article?
Best regards.
Asbjørn Morell
Ok, if you don't want the intermediate nested resource (/articles) I wouldn't use the map.resources at all.
Try:
map.connect '/powerusers/:user_id/:article_title', :controller => 'articles', :action => 'view_by_title'
If I add...
map.resources :users, :as => "powerusers" do |users|
users.resources :entries, :as => 'article-title'
end
I get the routes below, which include the one you want...
(Substitute "articles" for "entries" for your situation.)
GET /powerusers(.:format) {:controller=>"users", :action=>"index"}
POST /powerusers(.:format) {:controller=>"users", :action=>"create"}
GET /powerusers/new(.:format) {:controller=>"users", :action=>"new"}
GET /powerusers/:id/edit(.:format) {:controller=>"users", :action=>"edit"}
GET /powerusers/:id(.:format) {:controller=>"users", :action=>"show"}
PUT /powerusers/:id(.:format) {:controller=>"users", :action=>"update"}
DELETE /powerusers/:id(.:format) {:controller=>"users", :action=>"destroy"}
user_entries GET /powerusers/:user_id/article-title(.:format) {:controller=>"entries", :action=>"index"}
POST /powerusers/:user_id/article-title(.:format) {:controller=>"entries", :action=>"create"}
new_user_entry GET /powerusers/:user_id/article-title/new(.:format) {:controller=>"entries", :action=>"new"}
edit_user_entry GET /powerusers/:user_id/article-title/:id/edit(.:format) {:controller=>"entries", :action=>"edit"}
user_entry GET /powerusers/:user_id/article-title/:id(.:format) {:controller=>"entries", :action=>"show"}
PUT /powerusers/:user_id/article-title/:id(.:format) {:controller=>"entries", :action=>"update"}
DELETE /powerusers/:user_id/article-title/:id(.:format) {:controller=>"entries", :action=>"destroy"}
Instead of nesting, would this work?
map.resources :users, :as => "powerusers"
map.resources :articles, :path_prefix => '/powerusers/:user_id'
I think it won't but a quick test would tell better :)

Resources