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.
Related
I come from this post:
Rails using puma, change localhost:3000 to localhost:3000/example
I have fixed this issue, but now I receive "Method not allowed" when I do a post request. I have been reading and tried this solution:
Post returns 405 method not allowed
I know where is the problem: If I put in application.rb lines 1- and 2-, all assets are shown correctly and post methods aren't doing it. If I comment these lines, methods works but assets don't.
Application.rb:
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }
config.action_dispatch.rescue_responses["BadTaste"] = :bad_request
1- config.action_controller.asset_host = "https://www.sevilla.org"
2- config.assets.prefix = '/autorizaciones-movilidad'
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
Routes:
Rails.application.routes.draw do
#resources :assets, path: '/autorizaciones-movilidad'
scope "/autorizaciones-movilidad" do
get 'vehicles/new'
get 'vehicles/create'
...
get 'vehicles/update'
end
end
Controller structure:
Don't know how to solve it.
It is deployed with a proxy server (in localhost it was working ok)
I'm new to programming and I have this problem that I can't find an answer for - I've saved an update in sublime and haven't been able to connect to http://localhost:3000 (on OS X 10.9.5)
I keep getting a syntax error when I try to restart the server or migrate the database:
config/routes.rb:63: syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)
Rails.application.routes.draw do
resources :posts do
resources :comments, only: [:create]
end
# root 'welcome#index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
It looks like you're missing the initial declaration of the routes file. See the example below. You are getting this error because at the bottom of the file there is an end and it does not know what you're trying to end.
Rails.application.routes.draw do
#Routes Go Here
end
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 am using devise-invitable gem in my application. If the user exists in the application and he clicks accept invitation link he should be redirected to sign in page other if new user clicks the link he should be redirected to sign up page. I am not getting how I can override the after_accept_path_for method for that...Where and how can I override this method, can some one please help me in this?
Following https://github.com/scambra/devise_invitable/ link
I think you might want to re-read the documentation, your question is answered in the docs, just not all in one place.
Here are the two sections that concern your question:
https://github.com/scambra/devise_invitable#configuring-controllers
https://github.com/scambra/devise_invitable#integration-in-a-rails-application
Basically you're going to add a controller for invitations and add routing information for that controller (app/controllers/users/invitations_controller.rb) like this:
class Users::InvitationsController < Devise::InvitationsController
def after_accept_path_for
"some path you define"
end
end
Then you'll change your routes.rb to tell devise to use your invitations controller like:
devise_for :users, :controllers => { :invitations => 'users/invitations' }
With devise 2.1.2 and devise_invitable 1.1.8, the page you end up on after setting password from the invitation link depends on what you set the root path for your devise resource in the config/routes.rb so #trh's answer doesn't work with this version (I tried and failed). From the devise code comments:
# The default url to be used after signing in. This is used by all Devise
# controllers and you can overwrite it in your ApplicationController to
# provide a custom hook for a custom resource.
#
# By default, it first tries to find a valid resource_return_to key in the
# session, then it fallbacks to resource_root_path, otherwise it uses the
# root path. For a user scope, you can define the default url in
# the following way:
#
# map.user_root '/users', :controller => 'users' # creates user_root_path
#
# map.namespace :user do |user|
# user.root :controller => 'users' # creates user_root_path
# end
#
# If the resource root path is not defined, root_path is used. However,
# if this default is not enough, you can customize it, for example:
#
# def after_sign_in_path_for(resource)
# stored_location_for(resource) ||
# if resource.is_a?(User) && resource.can_publish?
# publisher_url
# else
# super
# end
# end
In my case, I had two different namespaces with one namespace called "portal" which I was using a "PortalUser" class for the user. For Rails 3.2, I simply declared this line in my routes.rb:
get "portal" => 'portal/dashboard#index', as: :portal_user_root
The important note is the naming, "portal_user_root" which is the underscored name of PortalUser class name. Simply setting this named route is all that's needed for your devise_invitable to redirect as desired.
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://.