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" %>
Related
I am getting this error:
Routing Error
uninitialized constant RegistrationsController
This is my log:
Started GET "/registrants/1/registrations/new" for 127.0.0.1 at 2012-07-03 00:55:44 -0500
ActionController::RoutingError (uninitialized constant RegistrationsController):
Rendered /.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (8.5ms)
This is the part of my routes.rb that is appropriate:
registrant_registrations GET /registrants/:registrant_id/registrations(.:format) {:action=>"index", :controller=>"registrations"}
POST /registrants/:registrant_id/registrations(.:format) {:action=>"create", :controller=>"registrations"}
new_registrant_registration GET /registrants/:registrant_id/registrations/new(.:format) {:action=>"new", :controller=>"registrations"}
edit_registrant_registration GET /registrants/:registrant_id/registrations/:id/edit(.:format) {:action=>"edit", :controller=>"registrations"}
registrant_registration GET /registrants/:registrant_id/registrations/:id(.:format) {:action=>"show", :controller=>"registrations"}
PUT /registrants/:registrant_id/registrations/:id(.:format) {:action=>"update", :controller=>"registrations"}
This is my Registrations Controller, which is at /app/controllers/portal/registrations_controller.rb:
class Portal::RegistrationsController < ApplicationController
def create
#registrant = current_member.registrants.find(params[:registrant])
#registration = #registrant.registrations.build
respond_to do |format|
if #registration.save
format.html { redirect_to root_path, :notice => "Registration successfully created!" }
format.json { head :ok }
else
format.html { redirect_to root_path, :notice => "There was a problem with the registration." }
end
end
end
end
I get this error when I call new_registrant_registration_path(registrant) from a view in Portal#index.
Edit:
For completeness, here is my routes.rb file in its entirety before the solution was found. Radar mentioned the gist I posted, but I figured I would put the code here in case that gist is deleted.
MyApp::Application.routes.draw do
match 'admin' => 'admin/dashboard#index', :as => :admin_root
match 'portal' => 'portal#index', :as => :member_root
namespace 'admin' do
match 'profile' => 'profile#show', :as => :profile
resources :members
resources :admins
resources :packages
resources :products
resources :levels
resources :lessons
resources :registrations
resources :registrants, :controller => 'customers/registrants'
resources :locations
resources :schools
resources :terms
resources :customers
resources :invoices
resources :payments
end
namespace 'member' do
resources :registrations
end
match 'register' => 'member/registrations#new', :as => :signup
match 'login' => 'member_sessions#new', :as => :login
match 'logout' => 'member_sessions#destroy', :as => :logout
match 'admin/login' => 'admin_sessions#new', :as => :admin_login
match 'admin/logout' => 'admin_sessions#destroy', :as => :admin_logout
match 'member/activate/:activation_code' => 'member/activations#create', :as => :member_activation
match 'member/:id/activate/resend' => 'member/activations#resend', :as => :resend_member_activation
match 'member/:id/activate' => 'member/activations#new', :as => :new_member_activation
match 'member/password/reset/begin' => 'member/password_resets#new', :as => :new_member_password_reset, :via => :get
match 'member/password/reset' => 'member/password_resets#create', :as => :member_password_resets, :via => :post
match 'member/password/:token/finish' => 'member/password_resets#edit', :as => :edit_member_password_reset, :via => :get
match 'member/password/reset' => 'member/password_resets#update', :as => :member_password_reset, :via => :put
match 'admin/packages/new/:partial' => 'admin/packages#new'
resources :admins
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
resource :admin_session
resource :member_session
root :to => 'portal#index'
end
To make things clear: marcamillion came into #rubyonrails and Freenode and shared his config/routes.rb. That's where I figured out the error that he was making and then figured out the answer.
Two things.
First: Admin root route
Rather than defining the root route for your admin namespace like this:
match 'admin' => 'admin/dashboard#index', :as => :admin_root
Define it inside the namespace :admin call like this:
namespace 'admin' do
root :to => "dashboard#index"
end
This will automatically define it as admin_root_path and admin_root_url, so you won't need to do that, and it will automatically prefix the controller with the admin/ prefix, so you won't need to do that either.
Second: Referencing RegistrantsController
You have this currently in your config/routes.rb:
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
What this will do is define resources routes for registrants correctly using the Portal::RegistrantsController, but won't define the nested routes underneath this using Portal::RegistrationsController. What it will do is attempt to use RegistrationsController, which is why you're getting that error.
To fix this, I'd recommend you use the scope method, like this:
scope :module => Portal do
resources :registrants do
resources :registrations
end
end
The scope method, when used in this way, will tell Rails that the controllers for the routes inside the block are under the Portal namespace. This means that it will know that resources :registrants is for Portal::Registrants and that resources :registrations is for Portal::Registrations, thereby fixing the error that you're seeing.
What if you change this line:
class Portal::RegistrationsController < ApplicationController
to:
class RegistrationsController < ApplicationController
?
This is strange. I think, your controller should be located at /app/controllers/registrants/registrations_controller.rb and looks like this:
class Registrants::RegistrationsController < Registrants::ApplicationController
def create
#registration = registrant.registrations.build
...
end
end
class Registrants::ApplicationController < ApplicationController
def registrant
current_member.registrants.find(params[:registrant_id])
end
end
Registrants::ApplicationController (/app/controllers/registrants/application_controller.rb) controller aim is to avoid code duplication.
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.
I'm currently getting the error:
No route matches [GET] "/tenant_admin"
I was using something like:
http://example.com/accounts/1/tenant_admin
but I'm now passing the account id as a subdomain;
http://AccountName.example.com/
Is it possible to make the url work like this:
http://AccountName.example.com/tenant_admin ?
Routes.rb
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users
resources :sessions
resources :password_resets
resources :accounts do
resources :tenant_admin
end
constraints(Subdomain) do
match '/' => 'accounts#show'
end
root :to => "welcome#index"
You have to put your tenant routes under resources :accounts and constraints(Subdomain). I don't recommend using copy and paste but a lambda instead.
tenant_routes = lambda do
resources :tenant_admin
end
resources :accounts do
tenant_routes.call
end
constraints(Subdomain) do
tenant_routes.call
end
I have a problem to upgrade my routes.rb file.
I converted this route:
map.resource :index, :controller => "users", :action => "index"
By
resource :index, :to => "users#index"
There is no error raised but the page does not display correctly.
Does anyone have an idea?
Update:
I have this instruction:
resources :users
Here is my routes.rb file:
JNetExams::Application.routes.draw do |map|
match 'login', :to => 'user_sessions#new', :as => 'login'
match 'logout', :to => 'user_sessions#destroy', :as => 'logout'
root :to => 'user_sessions#new'
resources :groups
resources :students
resources :users
resources :responses
#map.resource :index, :controller => "users", :action => "index"
resource :index, :to => "users#index"
map.resource :account, :controller => "users"
#resource :account, :controller => "users"
resource :user_session
resource :student
resource :user
map.resource :professor, :controller => "professors", :action => "index"
#resource :professor, :to => "professors#index"
map.connect ':controller/:action/:id'
#match ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
#match ':controller/:action/:id.:format'
#map.connect '*url', :controller => :users, :action => :index
match '*url', :to => "users#index"
#map.connect '/user_sessions/show', :controller => "user_sessions", :action => "show"
match '/user_sessions/show', :to => "user_sessions#show"
How can I upgrade this routes file to Rails 3?
resources :index provides paths for an index resource. So new_index_path will point to the new action in the IndexController. If you do not have an index resource, you should not be using resources to define a path to "users#index". This path is already given to you as users_path through resources :users. If you are looking to set an index page for your application, you can achieve that using root :to => "users#index".
The problem of things not displaying correctly is vague and I employ you to throw more light on that. We are here to help after all.
Do you have a resource called index?
This looks more like it should be
resources :users
If you need more help, please show the whole routes file.
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?