Scoped views do not work - ruby-on-rails

I have read all stackoverflow posts and the document on https://github.com/plataformatec/devise concerning the scoped Devise views in a Rails application.
I have a single model Admin. Later I am planning to add other models such as User. My problem is that my scoped views do not work. Here is what I have done:
I modified the file config/initializers/devise.rb: added config.scoped_views = true.
Then I generated a session view (using rails g devise:views -v sessions) new.html.erb, modified it and put this file in the folder app/views/admins/sessions.
I restarted the Rails server and followed the http://0.0.0.0:3000/admin/sign_in.
Nothing changed.
Then I put the file new.html.erb into the folder app/views/admins/sessions/new, again no effect.
Additional info: routes.rb contains devise_for :admin.
Does anyone have idea what I am missing?

The route should be
devise_for :admins
If you want to keep the route, Change the view folder to app/views/admin/

Related

How to change the name given with devise ?

I'm currently working on my personal website.
By adding devise gems to my code, I'va made a mistake.
I've written :
rails generate devise MODEL
and I want :
rails generate devise User
I know I could just start over, but I want to know first if it is possible to change this and if it is, how could I do that ?
Many thanks,
Raphaƫl.
Remove the table:
rake db:rollback VERSION=versionNumberOfMigration
Remove the configuration:
rails destroy devise:install
Remove your User model:
rails destroy devise MODEL
Check the references to devise in your routes.rb, controllers and views.
Also check for the following code snippets in your project:
devise_for (routes.rb)
before_action :authenticate_MODEL! (controllers)
MODEL_signed_in? (controllers, views) current_MODEL
MODEL_session (controllers, views)
Have you tried
rails destroy devise MODEL

Ruby on Rails, Routing

Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.

Rails ActiveAdmin Routing new Resources

I'm just starting out with Rails and I decided to try out ActiveAdmin last night. I was able to register a new resource name 'Pages' in my ActiveAdmin app, but there's one thing I can't figure out how to customize with it.
I create a new Page with ActiveAdmin, but it's published within the admin/.. path.
(e.g. mydomain/admin/page/1)
How do I change the routing so the page can be viewed at mydomain/page/1?
Are you able to change the routing of existing resources in ActiveAdmin?
I'm very new at Rails so I assume this is a pretty easy fix. I plan to run through some more tutorials/books so I can better understand routing.
You can change the default admin namespace.
To do so you have to go to config/initializers/active_admin.rb file and find the following configuration:
# Default:
# config.default_namespace = :admin
Uncomment the line and set the default_namespace to whatever you need.
However, if you need to turn off the namespace at all, you will have to set the default_namespace to false:
config.default_namespace = false
This will allow you to run the AA from the root.
By doing so be aware of changes in routes:
if changed the namespace to hello, the admin_games_path becomes hello_games_path;
if changed to no namespace, use normal routes: admin_games_path becomes games_path.

How to customize the default login page in ActiveAdmin?

The Problem
I'm trying to customize the default login page in ActiveAdmin, but I'm having trouble getting the customizations to go through.
What I've tried
A commenter on this RailsCasts episode suggests copying this file to app/views/active_admin/devise/sessions/new.html.erb and customizing it from there. However, doing so does not seem to replace the default login form.
I also tried replacing devise_for :admin_users, ActiveAdmin::Devise.config with devise_for :admin_users as the commenter suggests, which seems to point the routes to the right place, but I get a bunch of server errors related to none of the ActiveAdmin variables being recognized in this context.
I've searched the docs, but I haven't been able to find documentation around customizing the login form in particular.
Question
What's the best way to go about customizing the login form in ActiveAdmin?
With the default ActiveAdmin config where the Devise resource is admin_user, your new.html.erb should go in app/views/admin_users/sessions/new.html.erb instead.
An easy way to copy out all the Devise templates is to do rails g devise:views admin_users, though it turns out ActiveAdmin comes with its own versions of these views: https://github.com/gregbell/active_admin/tree/master/app/views/active_admin/devise
If you're additionally trying to change the layout that Devise's new.html.erb is rendered with, you can copy the layout file out from ActiveAdmin into app/views/layouts/active_admin_logged_out.html.erb
The current layout file used for the login page is here:
https://github.com/gregbell/active_admin/blob/master/app/views/layouts/active_admin_logged_out.html.erb
First copy all the devise views to your app:
rails g devise:views admin_users
Second add config.scoped_views = true inside the config/initializers/devise.rb file will do the trick.
In this way you don't need to override active_admin templates.

Issue with controller generation in rails

New to Ruby on Rails so this may be a stupid question. I have an app and I can bundle my gems without issue. So now I want to add some mostly static pages. I try to generate a controller for them with rails generate controller MostlyStatic page1 page2. This should generate a controller named mostly_static and pages named page1 and page2. Instead, I throw an error. Apparently the generate command is trying to connect to the database, which I have not yet created. There is nothing in these pages that should be a database table, so I'm a bit confused as to why the database is being brought into the process at this juncture. I've looked through various tutorials and none say that a database is required to generate controllers for static pages. So... what am I missing? Do I need to create the database first just to generate static pages? And, if so, will subsequently dropping any tables created by that generation impair the function of my app? I really don't want a bunch of useless tables for static pages hanging around. Is there a way to generate these pages and controllers without the database?
You are not following the convention for generating controllers. Generating a controller will not create a database table. You have to do that by calling rails generate model, rails generate resource or rails generate scaffold.
So you want a controller for a few static pages. Try this
rails generate controller static_pages home help contact
Notice the generator is plural and snake case (static_pages). this will generate the static controller and the home.html.erb, help.html.erb, and contact.html.erb pages
Now you can access the pages with these actions in the controller
def home
end
def help
end
def contact
end
Also need to make sure the routes are set up
# routes.rb
match '/home', to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
No database is set up and you can visit the pages. Thats all you need to do. just follow the conventions,like plural controllers and singular models and rails takes care of the details. Hope this gets you started
UPDATE
in response to the comments here is the standard output of generating a controller. Note my example used haml instead of erb, but there is nothing related to the database in the output.
rails g controller static_pages home help contact
create app/controllers/static_pages_controller.rb
route get "static_pages/contact"
route get "static_pages/help"
route get "static_pages/home"
invoke haml
create app/views/static_pages
create app/views/static_pages/home.html.haml
create app/views/static_pages/help.html.haml
create app/views/static_pages/contact.html.haml
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
create spec/views/static_pages
create spec/views/static_pages/home.html.haml_spec.rb
create spec/views/static_pages/help.html.haml_spec.rb
create spec/views/static_pages/contact.html.haml_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
create spec/helpers/static_pages_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
For anyone stumbling across this question, the correct answer is that the database need not exist, but it must be properly configured as if it did exist in the config file. Generating the controller does not actually create the database.

Resources