Simple question.
I'm using Rails 4.1.4 and Devise 3.3.0 for my app.
I'm trying to generate Devise's controllers so I can override some behaviour.
Documentation says to run...
rails generate devise:controllers [scope]
... to generate controllers under app/controllers/scope so you can then modify them. But when I run the previous command it keeps saying that there is no generator devise:controllers:
Could not find generator devise:controllers.
Does anyone knows why?.
Thanks.
UPDATE
In fact, when I run...
rails generate
... to retrieve a list of the available generators, I get the following output for Devise generators:
Devise:
devise
devise:install
devise:views
So definitelly, the devise:controllers generator isn't there. Is there a way to add it?. How?.
Thanks.
SOLVED
I've just created the controller manually and make it inherit from Devise. For example:
class Users::RegistrationsController < Devise::RegistrationsController
# Override the action you want here.
end
This controller should live in app/controllers/users/registrations_controller.rb. If you have any other scope just go with app/controllers/scope/registrations_controller.rb. For example if you have an admin scope it would be app/controllers/admins/registrations_controller.rb.
Best.
UPDATE
Following the comment from blushrt, I forgot to mention that it is important to modify config/routes.rb to make Devise use the created controller for the specific resource. For example, for users, you should put in your config/routes.rb:
devise_for :users, controllers: { registrations: "users/registrations" }
That's it. Best.
To answer the OP's original question of "Does anyone knows why?"
The problem is that this generator is currently only available on Devise's master branch, as stated on this GitHub issue.
If you want to use this feature before it's published, you could add this to your Gemfile:
gem 'devise', git: 'https://github.com/plataformatec/devise'
https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers
You can run this command in your terminal.
bash <(curl -s https://raw.githubusercontent.com/foohey/cdc/master/cdc.sh)
Putting this here in case anyone else has this (silly) problem. I couldn't work out why a POST to /users kept routing to Devise::RegistrationsController#create rather than Users::RegistrationsController#create
The reason?
I had a typo in routes.rb
devise_for :users, controllers: { registations: 'users/registrations }
Note I had registations instead of registrations
So be wary you may have a typo in your route somewhere
Related
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
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.
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/
I just installed Ruby on Rails and created a scaffold called posts. RoR generated controllers and other required files for me.
I created a new method in posts_controller, but I can't access it. I looked at other methods that are in the controller and looks like I need to access them by /posts/[MY POST ID]/[MY METHOD NAME].
Assuming I created my custom method hello in the controller, how do i access it?
I looked at routes.rb, but there's no configuration for it.
Updated:
I understand that I can manually configure it in routes.rb, but how do all the other methods work? For example, I have "edit", and "update" methods in the "posts_controller.rb" controller. How do those two methods work without configuring routes?
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
I can't find a configuration that matches /posts/[0-9]/edit pattern.
The documentation you're looking for is Rails Routing From the Outside In. Once you've read this you'll understand everything Rails does to take your request and point it at method in your controller.
You need to add a route for it to routes.rb. For example:
# Previous routes
# resources :posts
# Updated routes
resources :posts do
get "hello", :on => :member
end
Have a look at this Rails guide about routing, it should help you understand Rails routing.
This will give you a good head start on the routes: http://guides.rubyonrails.org/routing.html
Not every method you make will have its own path, rails is built on the rest principle, and your scaffold created methods in the post controller that follow those paths, like index, show etc....
You can force your method to have a route added to it, but in reality you rarely actually need to do so as following the convention is far easier.
In Rails 3.x
match 'posts/hello' => 'posts#hello'
Available at example.com/posts/hello
When you used scaffold to generate post, it added a line resources :posts in your routes.rb file. That line configures routes for all the controller actions that were generated. As Caleb mentions above, not every action has a dedicated path. A single path can correspond to multiple actions because rails also takes into account the HTTP method. So, for instance, the path /posts with the HTTP method GET corresponds to the controller action index, while the same path with the HTTP method PUT corresponds to the controller action update. You can see these associations when you run rake routes from the console. I agree with Jordan and Caleb that the Rails Guides is a good read and will help you understand routes.
I'm trying to write my first rails 3 gem - everything works well, except for routes - I can't seem to get them working. It's possible this is a very simple error - as mentioned, it's my first experience with engines. The gem itself is very, very basic - literally just one scaffold
My gem's config/routes file:
class ActionController::Routing::RouteSet
resources :frogs
end
...And when I try to start the server, I get the following error:
/home/john/.rvm/gems/ruby-1.9.2-p0/gems/cancandevise-0.1.0/config/routes.rb:3:in
<class:RouteSet>': undefined method
resources' for
ActionDispatch::Routing::RouteSet:Class
(NoMethodError)
Any suggestions much appreciated. At the present moment, the gem is nothing more than a very basic rails-generated 'frog' scaffold
Cheers,
- JB
#marcgg, I believe that's the syntax for a regular rails app, but I think he's talking about an engine.
#unclaimedbaggage, your engine/gem routes file should look like this:
Rails.application.routes.draw do |map|
resources :frogs
end
I made an example engine that touches on all the common setup issues I encountered when creating my first gem, you might find it helpful to reference:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/
I'm not sure if I get why you're using a routeset. What file did you show? Did you try this:
YourApp::Application.routes.draw do |map|
resources :frogs
end
More info here: http://asciicasts.com/episodes/203-routing-in-rails-3
Just wanted to add an alternative here, as I'm not sure #Keith Schact is doing it the conventional way, this worked for me:
MyEngine::Engine.routes.draw do
resources :frogs
end
then in the application that requires the gem:
mount MyEngine::Engine => '/my_engine', :as => :some_namespace
The url you will get is then:
http://myserver.com/some_namespace/frogs