I'm running a rails 3 app at the root level in a phusion passenger environment (CentOS, apache) and having difficulty getting passenger to find some routes, although rake routes shows the routes correctly. Everything works fine in development (i.e. using rails server instead of phusion passenger in apache).
I have an admin section to my app with a login page. The main part of the app works, but everything under the admin section is inaccessible because I get a 404 instead of the login page (when I disable login I can access the admin pages). My apache config is
<VirtualHost *:80>
ServerName foo.bar.com
DocumentRoot /var/www/apps/myapp/current/public
<Directory /var/www/apps/myapp/current/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
My login process is implemented as a before_filter in an admin controller:
class Admin::AdminController < ApplicationController
# login disabled for testing
before_filter :require_login
def require_login
#current_user ||= User.find_by_id(session[:user_id])
redirect_to admin_login_path unless #current_user
end
end
My routes file has
Mpf::Application.routes.draw do
secure_protocol = "https://"
...
namespace "admin" do
...
match "login" => "user_sessions#new", :as => :login, :constraints => { :protocol => secure_protocol }
...
end
...
end
and when I run rake routes I get
admin_login /admin/login(.:format) {:protocol=>"http://", :action=>"new", :controller=>"admin/user_sessions"}
BUT when I try to access http://foo.bar.com/admin I get a 404 and the log shows
Started GET "/admin/login" for iii.iii.iii.iii at 2011-07-13 07:20:41 -0400
ActionController::RoutingError (No route matches "/admin/login"):
As far as I can tell it should be working... except for the fact that it's not. Any help would be greatly appreciated!
Have you tried accessing with https://? It looks like you provided constraints to prevent access from http:// and the link you posted references http://.
Related
I have several static erb pages being served up in a ruby rails 4 site via the high voltage gem:
get '/about' => 'high_voltage/pages#show', id: 'about'
get '/contact' => 'high_voltage/pages#show', id: 'contact', :protocol => "https"
get '/privacy' => 'high_voltage/pages#show', id: 'privacy'
This all works well and good, except that the /contact route doesn't redirect or force SSL on, it is happy with whatever protocol is used.
I host the site on engine yard, attempting to put :force_ssl only or variants in the route line resulted in failed deployments - high voltage uses a slightly different set of arguments than normal routes so I suspect there is a conflict somewhere.
Anyone use highvoltage and SSL with rails 4 for select static pages (not the whole site)? Example routes line please.
You can achieve this by overriding the HighVoltage#PagesController see the override section of the documentation.
It might look something like this:
class PagesController < ApplicationController
include HighVoltage::StaticPage
before_filter :ensure_secure_page
private
def ensure_secure_page
if params[:id] == 'contact'
# check to make sure SSL is being use. Redirect to secure page if not.
end
end
end
Next disable the routes that HighVoltage provides:
# config/initializers/high_voltage.rb
HighVoltage.routes = false
Then in your application's routes file you'll need to set up a new route:
# config/routes.rb
get "/pages/*id" => 'pages#show', as: :page, format: false
I recently added a subdomain constraint to my Rails routes file
constraints(:subdomain => 'new') do
devise_for :customers do
get "/customers/sign_up" => "registrations#new"
post "/customers" => "registrations#create"
put "/customers/:id" => "registrations#update"
end
match '/' => 'roxy#index'
namespace :roxy, :path => '/' do
resources :customers
resources :surveys
end
end
In order to test the subdomain routing constraint locally, I added this line to my hosts file.
127.0.0.1 new.localhost.local
Now, I test my app in my browser at the URL new.localhost.local:3000. It takes about 10 - 15 seconds to load every page, which is unreasonably slow. If I remove the subdomain constraint and just go to 127.0.0.1:3000, everything is zippy and fast again.
What am I doing wrong? I'm new to Rails, so please tell me if there is a better way to do subdomain routing in rails, or if there is a setting I need to configure.
Figured it out. It's nothing to do with Rails or subdomains or thin. Turns out, unlike other unixy-things, OS X reserves the .local TLD for mDNS functionality. For every page, the DNS resolution was timing out before loading my app. So I just changed my /etc/hosts file to
127.0.0.1 new.localhost.dev
and everything's working great now.
Read more: http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/
I was recently tasked with segregating my existing Rails 3 application into a standard and premium version. The premium version will have all the existing functionality with the addtion of a new layout, and new functionality. Ideally I want premuim users to access this site byway of prefixing the current url with a subdomain, e.g, "premium.somesite.com"/ The standard version of the website I don't want to change and is still accessible by the default url.
If possible I would like to simply add some logic to exiting views and controller to determine how the request is treated on whether or not a subdomain is being used .
I tried following the Railcast subdomains in rails 3, however he's using subdomains in context of a blog model and database attributes where as I wanted mine to be application-wide. Is this even possible with subdomains? How can I get started?
What I've tried:
Added an additional VirtualHost to my apache httpd.conf file used by passenger:
<VirtualHost *:80>
ServerName premium.mydomain.com
DocumentRoot /somewhere/../myapp
<Directory /somewhere/public>
Allow from all
Options -Multiviews
</Directory>
</VirtualHost>
Added the following to my routes.rb
class PremiumSubdomain
def self.matches?(request)
request.subdomain == "premium"
end
end
MyApp::Application.routes.draw do
constraints(PremiumSubdomain) do
#all of my routes...
end
end
What happens:
Trying to navigate to my site with the premium subdomain results in a domain not found, adding the constraint to my route.rb prevents the rest of the application from rendering anything on my default url.
So it turned out that while I did have everything configured on apache appropriately we were using a third-party for our DNS server so it was simply a matter of adding a record for our sub-domain and everything else was peachy.
Is there a way to set the default root routes in rails to be one of my controller action and showing in the url rather than the root "/" ?
Say I have a "Computer" controller with a "index" action.
When my user login to my application, I want the url to be
http://localhost:3000/computer/index rather than http://localhost:3000/
root :to => "computers#index"
does the latter one, how can I make the default root url to be something like the prior one ?
UPDATE: a better way would be
root :to => redirect(/path)
The simplest way would be to change which URL your users get re-directed to after they successfully log in.
You could also add a forced re-direct in your controller:
# routes.rb
root :to => "computers#force_redirect"
# computers_controller.rb
def force_redirect
redirect_to '/computers/index'
end
I am writing my first application in rails and here is what I did
C:\Personal\rails\demo>ruby -v
ruby 1.9.2p136 (2010-12-25) [i386-mingw32]
C:\Personal\rails\demo>rails -v
Rails 3.0.5
C:\Personal\rails\demo>rails generate model book
invoke active_record
create db/migrate/20110325190010_create_books.rb
create app/models/book.rb
invoke test_unit
create test/unit/book_test.rb
create test/fixtures/books.yml
C:\Personal\rails\demo>rake db:migrate
(in C:/Personal/rails/demo)
== CreateBooks: migrating ====================================================
-- create_table(:books)
-> 0.0000s
== CreateBooks: migrated (0.0000s) ===========================================
C:\Personal\rails\demo>rails generate controller admin
create app/controllers/admin_controller.rb
invoke erb
create app/views/admin
invoke test_unit
create test/functional/admin_controller_test.rb
invoke helper
create app/helpers/admin_helper.rb
invoke test_unit
create test/unit/helpers/admin_helper_test.rb
Then i edited the admin_controller.rb as follows:
class AdminController < ApplicationController
scaffold :book
end
Here is the routes.rb file
Demo::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
end
However, when I go to http://localhost:3000/admin, I get a "No route matches "/admin"" error. I noticed that my routes.rb has only the commented lines. Did I do something wrong?
You have not added a route for admin, why are all your routes commented out.
if admin is a resource add this line
resources :admin
Also in your controller you will need an index method and index view file because http://localhost:3000/admin will take you there
Try removing the comment from this line:
match ':controller(/:action(/:id(.:format)))'
Note: This answer is for the case when above mentioned points are met yet you keep seeing this error. It was Rails 3.2 and Ruby 2.3.8
Problem definition:
I successfully logged in using admin creds. When page landed on /admin I saw No route matches [GET] "/admin" error page. I hit my head everywhere and used all my knowledge. Also, generated routes and grep but no admin route was published.
$ rake routes | grep admin
but only devise related admin routes were published. And then I read the warning about the routes which I noticed later, which goes like
ActiveAdmin: ActiveAdmin::DatabaseHitDuringLoad: Your file, app/admin/account_tags.rb (line 4),
caused a database error while Active Admin was loading. This is most common
when your database is missing or doesn't have the latest migrations applied.
To prevent this error, move the code to a place where it will only be run
when a page is rendered. One solution can be, to wrap the query in a Proc.
Original error message: PG::UndefinedTable: ERROR: relation "account_tags" does not exist
Solution
I ran rake db:migrate and restarted the server, the problem was gone.