rails 3 route path building issue - ruby-on-rails

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

Related

Rails: Define routes manually after modifying to_param

I have been trying to implement a vanity url for user profiles, based on the example here: Rails 3: Permalink public profile. I have replaced the 'id' with username:
def to_param
username
end
However this has caused issues with my other routes. I have set them so that they match the default sets of routes exactly, when running 'rake routes'.
get '/users/' => 'users#index', :as => :users
post '/users' => 'users#create'
get '/users/new' => 'users#new', :as => :new_user
get '/users/:id/edit' => 'users#edit', :as => :edit_user
patch '/users/:id' => 'users#update'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
# for vanity url
get '/:id' => 'users#show', :as => :user
With this setup, trying to access delete and update routes give me 'no route matches' error. What is the proper way to specify these, and / or should I be doing this a different way? Any help is appreciated.
I think it's interresting and more readable to keep the resources syntax in the routes.rb, except for the show, which you can rewrite to customize user_path :
resources :users, :except => [:show]
# 2 possibilities for the show url
get '/users/:id' => 'users#show' # can be removed if you don't want to keep /users/:id url
get '/:id' => 'users#show', :as => :user
But change the controller to find user by username instead of id, for example
def show
#post = Post.find_by_username(params[:id]) # instead of Post.find(params[:id])
# ...
end

Trouble Upgrading Rails 2 Routes for a Redmine Plugin

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.

Can't access path specified by rake routes command

I can't seem to correctly understand the routing in rails 3.1.
(keeping in mind I'm working on a project that depends on the refinery cms gem)
In my routes I have the following:
Blog::Application.routes.draw do
resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]
scope :module => "refinery" do
scope(:path => 'refinery', :as => 'refinery_admin', :module => 'admin') do
resources :news, :as => :news_items, :controller => :news_items
#resources :news, :except => :show, :as => :news_items, :controller => :news_items
end
end
end
The output of the rake routes command is:
news_items GET /news(.:format) {:action=>"index", :controller=>"news_items"}
news_item GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
refinery_admin_news_items GET /refinery/news(.:format) {:action=>"index", :controller=>"refinery/admin/news_items"}
POST /refinery/news(.:format) {:action=>"create", :controller=>"refinery/admin/news_items"}
new_refinery_admin_news_item GET /refinery/news/new(.:format) {:action=>"new", :controller=>"refinery/admin/news_items"}
edit_refinery_admin_news_item GET /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/admin/news_items"}
refinery_admin_news_item GET /refinery/news/:id(.:format) {:action=>"show", :controller=>"refinery/admin/news_items"}
PUT /refinery/news/:id(.:format) {:action=>"update", :controller=>"refinery/admin/news_items"}
DELETE /refinery/news/:id(.:format) {:action=>"destroy", :controller=>"refinery/admin/news_items"}
The following code in my application leads to an error:
<%= form_for [:refinery, #news_item] do |f| %>
<% end %>
Telling me that the following path:
undefined method `refinery_news_items_path' for #<#<Class:0x0000010663c480>:0x00000106623980>
doesn't exist.
Any path from the rake routes command would just not work. I'm pretty confident that's it's a setting issue. I'm actually kind of writing a plugin to the RefineryCMS gem without actually using the plugin generator, I'm just building it as if it was a normal web app.
My project is hosted here for those who care to take a peak. http://github.com/mabounassif/blog
Anyone knows what might be the problem?
You're scoping your "refinery" scope as "refinery_admin" so when you did your rake routes, you got refinery_admin_news_items
if you take out that :as => 'refinery_admin' clause, your routes will return back to "normal"
It seems that the problem was with the way the Refinery CMS gem works. Apparently I shouldn't use the url helper right away, I should use the following instead:
main_app.new_refinery_admin_news_item_path

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 3 | Routing : How to rename resource title now?

Generally I have MenuItems model and trying to make '/menu_items(/:id(:/some_action))' URLs looks like '/menu(/:id(:/some_action))'
In Rails 2.3.5 it was
map.resources :menu_items, :as => :menu, :path_names => { :new => 'add' }
Now in Rails 3.0.3 I'm able to handle it using this huge paragraph of code
resources :menu_items, :path_names => { :new => 'add' }
match 'menu/' => 'menu_items#index', :as => :menu
match 'menu/add' => 'menu_items#new', :as => :new_menu
match 'menu/:id' => 'menu_items#show', :as => :show_menu
match 'menu/:id/edit' => 'menu_items#edit', :as => :edit_menu
But it looks incorrect because of huge amount of code.
Seems :as works like 2nd Rails' map.some_name now.
Any help/suggestions/guides? (Thanks)
http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
resources :menu, :controller => "menu_items", :path_names => { :new => "add" }
Output is quite close to what you're after:
menu_index GET /menu(.:format) {:controller=>"menu_items", :action=>"index"}
POST /menu(.:format) {:controller=>"menu_items", :action=>"create"}
new_menu GET /menu/add(.:format) {:controller=>"menu_items", :action=>"new"}
edit_menu GET /menu/:id/edit(.:format) {:controller=>"menu_items", :action=>"edit"}
menu GET /menu/:id(.:format) {:controller=>"menu_items", :action=>"show"}
PUT /menu/:id(.:format) {:controller=>"menu_items", :action=>"update"}
DELETE /menu/:id(.:format) {:controller=>"menu_items", :action=>"destroy"}
Another way
resources :menu, :as => :menu_items, :controller => :menu_items
This approach has the advantage that the exiting helper methods such as menu_items_path still work.

Resources