Namespaced controller redirect urls - ruby-on-rails

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.

Related

locale parameter changes his location in link

I have some serius problem, I created multiple language selection that works great, but the link is changing all the time.
From http://localhost:3000/en/products when I click such link
<%= link_to image_tag('en.png',:alt => 'description', :title => (I18n.t 'english') ), params.merge(:locale => :en) %>
To something like this after some time I am navigating inside app and clicking different links
http://localhost:3000/manufacturer_products?locale=en&manufacturer=Christophel
I believe that the problem is inside the routes.rb file.
This is my routes file.
root :to => 'home#index'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
get "about_us/index"
namespace :products do
resources :categories do
resources :products
end
resources :products, only: :index
end
namespace :products do
resources :manufacturs do
resources :products
end
resources :products, only: :index
end
get "about/index"
match ":locale/products/search" => "products#search"
match "/contacts", to: "contacts#index"
match "/products", to: "products#index"
match "/home", to: "home#index"
match "/about_us", to: "about_us#index"
match "/news", to: "news#index"
match "/manufacturer_products", to: "manufacturer_products#index"
match '/:locale' => 'home#index'
scope "(:locale)", :locale => /en|lv|ru/ do
resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products
end
I understand that I have to somehow merge namespace:products route with locale route, but I have no idea to start with, If somebody could give me a tip or smth :)
Thanks
I think your routes are not a problem.
params.merge(:locale => :lv) has the function of joining 2 hashes, so you should just use something like
params[:locale] || :en
I found solution, the thing was that in routes file I didn't have right order of the routes.
This route that handles routing with locales I had at the end off file. So it didn't work.
scope "(:locale)", :locale => /en|lv|ru/ do
resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products, :about_us, :contacts
end
I moved this code to begining of the file and now it works perfectly. :)

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.

Correct order of resources in Rails routes.rb

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.

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: Stack level too deep when using routes with ":only" or ":as"

I'm currently upgrading my Rails application from 2 to 3. Everything has been going fine until I finished with the routes.rb file. I'll include the contents below:
myApp::Application.routes.draw do
resources :forum_groups
resources :forums do
resources :topics
end
resources :topics do
resources :posts
end
match "confirm_user", :to => "users#confirm"
resources :users do
resources :comments
end
match "moderated_submissions", :to => "submissions#moderated"
resources :submissions do
resources :reviews
end
match "submissions/:id/download" => "submissions#download", :as => :download_submission
match "submissions/:id/trash" => "submissions#trash", :as => :trash_submission
match "submissions/:id/untrash" => "submissions#untrash", :as => :untrash_submission
match "submissions/:id/moderate" => "submissions#moderate", :as => :moderate_submission
match "submissions/:id/unmoderate" => "submissions#unmoderate", :as => :unmoderate_submission
match "submissions/:id/feature" => "submissions#feature", :as => :feature_submission
match "submissions/:id/unfeature" => "submissions#unfeature", :as => :unfeature_submission
#Will work on this later.
#map.user "users/:username", :controller => "users", action => "show"
#map.page "page", :controller => "static_page", :action => "show"
resources :static_pages do
resources :comments
end
resources :categories
resources :comments, :only => [:destroy]
resources :reviews, :only => [:destroy]
resources :features, :as => 'featured'
resources :announcements do
resources :comments
end
root :to => "pages#root"
match "browse", :to => "pages#browse"
match "login", :to => "sessions#new"
match "logout", :to => "sessions#destroy"
match "register", :to => "users#new"
match ":controller/:action/:id(.:format)"
end
The problem occurs when any one of these three lines are present:
resources :comments, :only => [:destroy]
resources :reviews, :only => [:destroy]
resources :features, :as => 'featured'
When any of these lines are present in the routes.rb file I get a, "stack level too deep" error when I try to start the server or the console.
If I remove the statements the application starts fine. If I remove the ":only" and ":as" statements, the application starts fine.
I have tried reordering the routes, and deleting them all except those three and the error still occurs. I have also tried creating a new application from scratch and inserting those three routes. In the case of the new application, it is able to start with those statements.
Any ideas where the problem may lie?

Resources