Devise: My customer data is not available anymore - ruby-on-rails

I made a big mistake and I'm not sure how to fix it.
I originally installed Devise the proper way, but today I decided to create a second instance of user using the following terminal line:
rails g controller Users index
Now I realize this was not the right thing to do, and I decided to destroy the generated code as with this command:
rails destroy controller Users
Now nothing works and I get the following error when accessing the sign up view:
Routing Error
uninitialized constant CustomersController
Try running rake routes for more information on available routes.
Here's my route file
devise_for :users
root :to => 'events#index'
What am I supposed to do in this case???

Related

Rails - using namespace controllers to organize files

I am trying to learn about namespacing.
I've asked a few questions on this topic previously, but I'm not understanding what is going on.
I have made a folder in my controller's folder called 'features'. In it, I have saved a file called app_roles_controller.rb.
The first line of that controller is:
class Features::AppRolesController < ApplicationController
The purpose of the features folder is so I can organise my files better (that's it).
In my routes.rb, I have tried:
resources :app_roles, :namespace => "features", :controller => "app_roles"
I have also tried:
namespace :features do
resources :app_roles
end
I have a model (top level) called app_role.rb and I have a views folder saved as views/features/app_roles which then has the index, show etc files in it. The table in my schema is called 'app_roles".
When I rake routes for app_roles, I get:
Paths Containing (app_role):
app_roles_path GET /app_roles(.:format)
app_roles#index {:namespace=>"features"}
POST /app_roles(.:format)
app_roles#create {:namespace=>"features"}
new_app_role_path GET /app_roles/new(.:format)
app_roles#new {:namespace=>"features"}
edit_app_role_path GET /app_roles/:id/edit(.:format)
app_roles#edit {:namespace=>"features"}
app_role_path GET /app_roles/:id(.:format)
app_roles#show {:namespace=>"features"}
PATCH /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
PUT /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
DELETE /app_roles/:id(.:format)
app_roles#destroy {:namespace=>"features"}
I can't understand what it is that I'm doing wrong.
When I try:
http://localhost:3000/app_roles#index
I get an error that says:
uninitialized constant AppRolesController
When I try:
http://localhost:3000/features/app_roles#index
I get an error that says:
No route matches [GET] "/features/app_roles"
I'm looking for a plain English explanation of how to set this up. I've tried the programming ruby book (several times over).
Please, can you help me understand what needs to happen to introduce organisational files in my rails app?
It looks like your other attempt was actually correct. As outlined in the Rails documentation, if you want to generate routes for the resource app_roles under the namespace features you can add the following to your routes.rb file:
namespace :features do
resources :app_roles
end
Now, you run rake routes you will see the following:
Prefix Verb URI Pattern Controller#Action
features_app_roles GET /features/app_roles(.:format) features/app_roles#index
POST /features/app_roles(.:format) features/app_roles#create
new_features_app_role GET /features/app_roles/new(.:format) features/app_roles#new
edit_features_app_role GET /features/app_roles/:id/edit(.:format) features/app_roles#edit
features_app_role GET /features/app_roles/:id(.:format) features/app_roles#show
PATCH /features/app_roles/:id(.:format) features/app_roles#update
PUT /features/app_roles/:id(.:format) features/app_roles#update
DELETE /features/app_roles/:id(.:format) features/app_roles#destroy
The first line for example means that if you make a GET request to /features/app_roles it will be routed to the index action in the features/app_roles controller.
In other words, if you visit http://localhost:3000/features/app_roles it will route the request to index action that is located in app/controllers/features/app_roles_controller.rb which it expects to have the class Features::AppRolesController.
So your app/controllers/features/app_roles_controller.rb file should look something like this:
class Features::AppRolesController < ApplicationController
def index
end
end

Auto-generate routes for scaffolded controller in Rails 4?

I'm trying to get a quick-and-dirty Ajax UI going for an app that already has its data model well in hand - it's basically been managed via rails console so far. Anyway, I thought I would start by auto-generating the missing controller logic that you would get from a rails g scaffold, only instead with rails g scaffold_controller for an existing controller.
It created the controller, and the views, and the assets.. but it didn't touch the routes at all! It didn't even try, didn't say "warning: routes.rb has been modified, not changing" or anything like that, and there's no mention of routes at all in the help output of rails g scaffold_controller.
So how do I say "Just give me the normal routes you would have given me if I started from scratch, please!"?
If I understand the question:
Please, open the config/routes.rb file, and inside the block (routes.draw) add the resources method with the table name (plural of model) as param. Like this:
MyApp::Application.routes.draw do
resources :products
... # rest of code
end
That define the routes for RESTful actions over products. You can read more here
At the console you can run: rake routes to see the available routes at your app.
Although this is asking about Rails 4 for long time ago, but with Rails 5, rails g scaffold_controller still won't auto-generate route, I did it with below monkey patch:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
patcher = Module.new do
extend ActiveSupport::Concern
included do
hook_for :resource_route, required: true
end
end
Rails::Generators::ScaffoldControllerGenerator.send :include, patcher

Rails Devise - Routes Error

I was installing the Devise gem, and everything seemed to be going according to tutorial. I got to the point where I was able to register a user, but then at some point I'm not sure what happened and I started getting routes errors:
No route matches [GET] "/projects"
Except, my routes file has been unchanged and has quite a few routes in it.
Any suggestions?
As requested the result of rake routes:
http://pastebin.com/FB17DhYq
If you can't restart/reload the server than try to reload the routes adding to the controller (some that works and you can hit it):
Rails.application.reload_routes! for Rails4 or ActionController::Routing::Routes.reload! for previous versions.
You can add just the line above or use this:
before_filter :reload_routes
private
def reload_routes
Rails.application.reload_routes! # for Rails 4
# ActionController::Routing::Routes.reload! # for previous
end

Rails generate controller (under a namespace)

I want to add a controller and its routes entry under a namespace (api) for which I am proceeding with rails generate api/Users my_method, which creates the files and entries as follows:
create app/controllers/api/users_controller.rb
route get "users/my_method"
invoke erb
create app/views/api/users
create app/views/api/users/my_method.html.erb
Everything worked fine apart from the routes entry. What I am assuming is it should create the routes entry as well under the correct namespace or it shouldn't create it at all, or I am doing something wrong.On the other hand when going with scaffold way it does correctly.
Is it something which we need to do it manually?
Using ruby 2.0 and rails 4 for the application.
Type in terminal
rails generate scaffold Api::User username email
rake db:migrate
this is part of result
class Admin::ServicesController < ApplicationController
# GET /api/users
# GET /api/users.json
def index
#api_users = Api::User.all
end
To do generate and I think you will understand everything, don't forget see new app structure :-), Good luck, and solve your problem quickly as possible.
You can namespace your routes in your config/routes.rb like this
namespace :api do
resources :user
end

Rails route parent ID name and CanCan issue

I'm trying to get a simple route working
/agenda_items/5/feed
To do this, I have the following route setup
resources :agenda_items do
member do
get "/feed", to: "comments#feed"
end
end
In each of my controllers, I'm using CanCan to handle the authentication and it works fine, however on this one action I'm having an issue, which I'm pretty sure is down to railsnaming generation. When I runrake routes`, the route above is produced as
feed_agenda_item /agenda_items/:id/feed(.:format) agenda_items/:id#feed
As far as I can tell, CanCan is expecting the :id parameter, to actually be :agenda_item_id so as a result, my parent resource isn't being loaded.
Is there any way I can get rails to change this so that CanCan will work without me having to manually load and authorize the resource, or is there a way I can get CanCan to change what it's looking for on certain actions?
The problem is that your routes are wrong. You try to create a member action for agenda items which routes to the comments controller. If you want a feed of all the commments from a single agenda item you should do something like this:
resources :agenda_items do
resources :comments do
collection do
get :feed
end
end
end
You should now get the following when running rake routes:
feed_agenda_item_comments /agenda_items/:agenda_item_id/feed(.:format) comments#feed

Resources