Correct order of resources in Rails routes.rb - ruby-on-rails

I keep getting strange errors because of the way my routes.rb file is organized. The latest one is that some function cannot find action "show" in model Relations controller (the action is obviously there). I guess this is because I am adding some custom actions via collection and something about the order in which the routes are declared is messed up.. Can somebody please have a look at this and say what is wrong?
YApp::Application.routes.draw do
require 'resque/server'
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match '/get_idx', :to => 'nodes#get_idx'
resource :relations do
collection do
post 'this_relation'
post "iframize"
end
end
resource :words do
get 'page/:page', :action => :index, :on => :collection
collection do
get 'front'
get 'index'
end
end
resource :recommendations do
collection do
get 'find_votes'
end
end
get "connotation/create"
get "connotation/edit"
get "connotation/update"
root :to => "words#front", :as => :homepage
resources :users, :user_sessions, :relations, :evaluation, :phrases, :metawords, :nodes, :recommendations, :words
mount Resque::Server.new, :at => "/resque"
match 'about' => 'words#index' , :as => :about
match 'contact' => 'keywords#index' , :as => :contact
end

You might have an issue with resource :relations. Rule of thumb is: if you use the plural resources, then the name of the resource must also be plural (i.e. :relations), if you use resource, in singular, than you should use singular for the resource name too (i.e. :relation).
Other possible problems: your indentation is off. Maybe it's just a copy-paste issue, but check it nonetheless, because you might have some unexpected nesting going on.
Also inspect rake routes CONTROLLER=relations. Compare that to the log of the failed request and see if every parameter matches up.

Related

what does this rails code do? :on => :member

please can you explainme what does this code do?
resources :products do
get :who_bought, :on => :member
end
the code complete is from the book pragmatig programing, but it not explain why we use that code, ":on => :member""
Depot::Application.routes.draw do
resources :orders
resources :line_items
post 'line_items/decrease'
resources :carts
get "store/index"
resources :products do
get :who_bought, :on => :member
end
root :to => 'store#index', :as => 'store'
thanks
passing :on => :member means that you are working on a specific record in the database, in this case products. so the url that route generates is
/products/:id/who_bought
which means that you want the get the product whose id is :id and process the who_bought action. the counterpart, :on => :collection, expects the action to work on a list of products so the url will look like
/products/who_bought
if you change member to collection. you can see that the route doesn't require an :id passed because it doesn't expect you to work on a single record.

Unable To Access Public Images Possible Routing Issue

I'm working on a project and trying to render some images sitting under the public directory in one of my show views. I'm able to access these images just fine on my index view, but it's not quite so easy for show views it would seem.
So lets say in my view I have something like the following:
<%= "<img src=\"images/16x16/actions/filesave.png\" class=\"icon" title=\"save\">"%>
This would work in my index view perfectly fine, but for some strange reason I get this routing error in my show view:
ActionController::RoutingError (No route matches [GET]
"/uploads/images/16x16/actions/filesave.png"):
I noticed that for some peculiar reason it's injecting the "/uploads/" route right before the "/images..." this is the cause of my problem and I can't figure out why or how to stop it. This only happens with my show views.
Now there's a lot going on in my routes.rb file, I know it's ugly but I do plan on going in there and cleaning it up when I get the chance.
resources :upload_images
get "upload_image/new"
get "upload_image/index"
get "upload_image/show"
get "upload_image/delete"
resources :help_categories
resources :global_configs
resources :competitions
match '/teams/register', :controller => 'teams', :action => 'register'
match '/teams/invite_users', :controller => 'teams', :action => 'invite_users'
match '/teams/view_invitations', :controller => 'teams', :action => 'view_invitations'
match '/teams/ignore', :controller => 'teams', :action => 'ignore'
match '/teams/leave_team', :controller => 'teams', :action => 'leave_team'
resources :teams
resources :competitions do
resources :matches
end
resources :registers
resources :players do
collection do
post :edit_individual
put :update_individual
get :results
end
end
resources :tournaments
resources :matches
resources :upload_categories
resources :uploads, :except => [:new]
match '/download/:id' => 'uploads#download'
devise_for :users do
match 'logout' => 'devise/sessions#destroy'
end
resources :users, :except => [:new] do
member do
get 'upload_files'
get 'delete_files'
end
end
resources :games
devise_for :videolinks
resources :topics do
collection do
get "mark_all_viewed"
end
member do
get 'show_new'
end
end
resources :posts do
member do
get 'quote'
get 'topic'
end
end
resources :forums do
member do
get 'confirm_delete'
end
end
resources :blog_entries, :except => [:index]
resources :categories
resources :videolinks
resources :competition_games
resources :competitions
resources :news
resources :events
match 'uploads/find_uploads' => 'uploads#find_uploads'
match 'uploads/add_upload_image' => 'uploads#add_upload_image'
match 'forum_root' => 'forums#index'
match 'upload_root' => 'uploads#index'
match 'user' => 'forums#index'
match 'news_root' => 'news#index'
match 'topic_post' => 'forums#index'
match 'quote_post' => 'forums#index'
match 'new_upload' => 'forums#index'
match 'videolinks/:id', :to => 'videolinks#show'
match 'register' => 'users#sign_up'
match 'login' => 'users#sign_in'
match 'users/find_users' => 'users#find_users'
match '/users/get_states/:country' => 'users#states'
match '/ban/:username' => 'users#ban'
match '/ban_user/:username' => 'users#ban_user'
match ':username' => 'users#show'
match ':username/edit' => 'users#edit'
match ':username/delete_files_all' => 'uploads#index'
match ':username/delete_files' => 'users#delete_files'
match ':username/upload_files' => 'users#upload_files'
match ':username/password/edit' => 'users#editpass'
match ':username/edit_file/:id' => 'uploads#edit'
match '/maketakeadmin/:username' => 'users#maketakeadmin'
match ':username/destroy' => 'users#destroy'
root :to => "home#index"
resources :categories do
member do
get 'confirm_delete'
end
end
Another developer worked on the upload section of this application and it uses paperclip. By default it saves uploads in the public directory and we didn't want that so he told me he did some weird hotfix to save uploads to a private directory off of the root of the application called "uploads". Not sure if this might have anything to do with it.
Needed a forward slash at the start of the path.
I think you should use something like this:
<%= image_tag "/images/16x16/actions/filesave.png", class: "icon", alt: "save" %>

Rails 3 Routes: Override Topics#show with Posts#index

Pretty simple question, but I can't seem to figure out how to do this, even after scouring the Rails Routing guide.
Assume Topics has nested resource Posts.
The Posts for a Topic are all listed in Posts#index (/topics/:topic_id/messages). Topics#show does not serve any purpose. I would like Posts#index to be retrieved when the request is for /topics/:topic_id, without having to stick a redirect in the Topics controller.
Thank you!
UPDATE
I was able to get the desired result with this:
routes.rb
match 'forums/:forum_id' => 'topics#index', :as => 'forum_topics', :via => :get
match 'topics/:topic_id' => 'messages#index', :as => 'topic_messages', :via => :get
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end
However, I'm not sure if this is the best method.
UPDATE 2
My method above breaks the other CRUD methods (like #create). Still looking for a solution to keep /messages out of the url.
add this route
get "/topics/:topic_id" => redirect("/topics/%{topic_id}/messages")
UPD
without redirecting:
get "/topics/:id" => "topic::Messages#index"
Or, if you're using shallow:
get "/topics/:id" => "messages#index"
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end

Namespaced controller redirect urls

i have probably a simple question. I have created a namespace panel with categories controller.
After creating or editing a category, rails redirects me to website.com/categories/:id instead of website.com/panel/categories/:id.
I've noticed that in the _form view, the #panel_categories argument of form_for() function points to /categories nor /panel/categories and that's causing this behaviour. Offcourse i can add a :url => '/panel/categories' param but i feel that it's not the best solution...
Can you provide me any better solution?
Thanks in advance
Files:
routes.rb:
Photowall::Application.routes.draw do
resources :photos
resources :categories
resources :fields
resources :users, :user_sessions
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
namespace :panel do
root :to => "photos#index"
resources :users, :photos, :categories, :fields
end
namespace :admin do
root :to => "users#index"
resources :users, :photos, :categories, :fields
end
end
categories_controller.rb:
http://pastebin.com/rWJykCCF
model is the default one
form:
http://pastebin.com/HGmkZZHM
form_for [:panel, #panel_category]
You can set the url to a route such as:
:url => panel_categories_path
I'm not sure what your route is, but this should work with your application.

resources :users, :as => 'members' equvalent in Rails 3

So in Rails2 you could setup a route like this:
resources :users, :as => 'members'
This way you still have your users_path but it matches /members
Now, how do you go about doing this in Rails3? :as parameter here means totally different thing and I can't find what makes it work as before.
I think what you're looking for is the :path option:
resources :users, :path => 'members'
Tested it on my app and the users_path still works, but matches /members as required. It doesn't match /users.

Resources