Creating Rails Controller In Sub-directory - ruby-on-rails

How do I create a rails Events controller in a different directory other than the default:
app/controllers/events_controller.rb
I need to create in app/controllers/api/events_controller.rb
I created the api sub-directory and did cd in terminal to api. When I created the controller it still generated in the default app/controllers/.
Thanks.

You can namespace your controllers (generated like this: rails g controller API::Events).
Put your controller in the api directory within your controllers directory and name the controller's class like this:
class API::EventsController < ApplicationController
More details discussed here: https://stackoverflow.com/a/9946410/1026898
If that is not what you want to do, rails tends to lean in the direction of not putting that controller in a different directory.
It doesn't hurt anything to do so, it's just a bit odd. The rails generators, by default, are built to put the controllers in the conventional directory.
If you want to change where they are generated you will have to update the generator.

To do that with rails generators:
rails g controller API::Events

Related

Controllers into completely different directory

I'm trying to build an admin panel to my rails application, but want to keep my admin controllers away from my other controllers. Is there anyway I can have a admin folder in my app folder which contains controllers just for admin stuff.
Thanks in advance.
Yes, sure.
You can put all the admin related controllers in app/controllers/admin/ directory.
Yes, you can do this by namespacing your controllers under an admin module.
The easiest way to set this up is to use the rails generator, and prefix your resource with "admin":
rails generate controller admin/user
Type rails g controller for specific helps.
Here's a page from the guide with more info: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
If you want to keep your admin completely separate, you can use an engine. To generate the engine, do:
rails plugin new admin --mountable
Then in your main app's routes file, you can mount the engine with:
mount Admin::Engine => "/admin"
See http://guides.rubyonrails.org/engines.html for complete details on engines.
Thats very simple, usually it makes sense to put those in app/controllers/admin but if you use this, you'll need to use a namespace. Rails will then autoload these classes.
It's a good practice to make an ApplicationController per namespace (I'm calling it base controller) like this:
module Admin
class BaseController < ApplicationController
end
end
and here's an exapmle controller:
module Admin
class ExampleController < Admin::BaseController
def example
end
end
end

Singleton controller in Rails 4.2

I wonder how to create singleton controller in Rails 4.2.
For example rails g scaffold Dashboard will generate dashboards_controller witch in my case has no sense because I need only one Dashboard so dashboard_controller is the thing I need.
I see there is an option -c to specify controller name, however I bet there was something like --singleton but is gone now.
So, the question is, should I use -c to override controller name or the "new Rails way" is to create plural controllers names, like dashboards_controller and then use router to point it to dashboard URL?
I don't know how to do it using a generator, but it's easy enough to generate with a plural name and then change it to singular manually.
Your route will be something like:
resource :dashboard, controller: 'dashboard', :only => ['show']
Your controller class should be renamed to DashboardController and the file name itself to dashboard_controller.rb. The view folder that holds your view files should also be singular - app/views/dashboard
The "Rails Way" is to go with plural controller names by default, but it's fine to use singular controller names when they make sense - which they certainly do in this case.
rails g controller dashboard seems what you are looking for.
$ rails g controller dashboard
create app/controllers/dashboard_controller.rb
invoke erb
create app/views/dashboard
invoke test_unit
create test/controllers/dashboard_controller_test.rb
invoke helper
create app/helpers/dashboard_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/dashboard.coffee
invoke scss
create app/assets/stylesheets/dashboard.scss

The class name that Rails generate controller generate

For some reason I can't run a rails generate controller on my machine at the moment, something is messed up but still I need a controller. So decided to create it by hand.
But I can't remember the conventions: when I was saying rails generate controller Therapeutics was it creating a
therapeutics_controller.rb
and a
therapeutics.html.haml
So I can create them by hand?
And what was the class name in the controller? Would it be class TherpeuticsController ?
app/controllers/therapeutics_controller.rb should be
class TherapeuticsController < ApplicationController
# define your actions here..
end
app/views/therapeutics => put in your views here
and that's it. if you need helper or model, simply create it by hand. the rails generators are very convenient, but understanding the conventions and creating it by your own gives you more flexibility.
for better understanding, simple check out the basics http://guides.rubyonrails.org/getting_started.html
Just ran it and this is what I got:
$new_project rails generate controller Therapeutics
create app/controllers/therapeutics_controller.rb
invoke erb
create app/views/therapeutics
invoke helper
create app/helpers/therapeutics_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/therapeutics.js.coffee
invoke scss
create app/assets/stylesheets/therapeutics.css.scss
$new_project

ruby on rails sub directory inside in the 'Views' main directory

am a newbie in ruby on rails and am stuck with a simple problem of routing.
I have my controller 'sub' and the 'Views' folder containing the add,edit,new erb files.
In my routes file, i have 'map.resources :subs'.
Until now, everything is fine.
Problem:
I moved the add,edit,new erb files into a subfolder called 'admin' inside the 'Views' main directory.
I have no idea how to call those erb files from that 'admin' subdir.
By default, it is looking for /app/views/subs/index.html.erb, and i want it to look in /app/views/subs/admin/index.html.erb
Please can anyone tell me how to do this.
Many many thanks
I suggest a different approach because it seems what you want to do is admin routing. In your routes.rb write
namespace :admin do
resources :subs
end
then put your views in the subdirectory views/admin/subs
also, put your controller in the subdirectory controllers/admin and namespace them with "Admin" too, e.g.
class Admin::StubsController < Admin::ApplicationController
your_code_goes_here
end
of course, then you need an application_controller.rb in the controllers/admin dir as well. But you cold also derive from ApplicationController then that is not necessary.
your controller can be called through the url /admin/subs
does that help?
You could explicitly render your templates within your controller actions, like this:
render :template => "subs/admin/index"
I'm a beginner in RoR.
What I wanted was to group all views (such as a mobile friendly version) in 1 folder but not end up with an extra namespace OR create new method in controllers. localhost:3000/posts calls:
class Post < ActiveRecord
and not
class Admin::Post < ActiveRecord
BUT load the views in views/android/posts/index.html.erb
Because this was my Google first hit, the link below is to an alternative answer that took sometime for me to find.
Rails: Elegant way to structure models into subfolders without creating submodules

Why is Scaffolding Not Working in Ruby on Rails?

I created a controller and a model. The controller is called "Admin" and the model is called "Album". I edited database.yml with proper info and did the rake db:migrate command which didn't return any errors and did migrate the db inside schema.rb. Inside the controller I wrote:
class AdminController < ApplicationController
scaffold :album
end
Next I started my server and went to http://localhost:3000/admin but instead of seeing the typical CRUD page I get the following error:
app/controllers/admin_controller.rb:3
Request
Parameters:
None
Show session dump
---
flash: !map:ActionController::Flash::FlashHash
{}
Response
Headers:
{"cookie"=>[],
"Cache-Control"=>"no-cache"}
Any idea why?
That syntax for scaffolding has been deprecated for quite some time. Nowadays, rails (versions 2.x) use the following method to scaffold a resource:
script/generate scaffold Album title:string date:date ...
That generates the scaffolding views (in app/views), the controller (app/controllers), standard tests (in test/) and, crucially, the required routes to make scaffolding work.
I believe the rails dev team took away the old syntax ("scaffold :resource") because no real application would ever leave a scaffold untouched, ie. you will always need some kind of customization. With the new syntax you can leave it untouched, but it is also much easier to customize.
If you really need your controller to be named admins, you can change the file config/routes.rb after generating the scaffolding. It makes no sense, though: Why should the URI to create a new album be called "/admins/new"?
If you are trying to create an admin area for an image album app, you are probably looking for namespaces (so you can have multiple different resources, controllers and views inside the "admin" namespace). To create an album resource within the admin namespace, write:
script/generate scaffold Admin/Album title:string date:date
In that case, your controller will be accessible as http://host/admin/albums.
Hm,
Normally you would have a controller and a model called Admin and the same thing would be about Album,
Take a look at this quick screen cast how a blog is done using scaffolding;
Creating a web-blog
the script/generate command seems not to work, someone has to provide ./script/generate , I think its a linux directory issue, you have to explicitly say you are starting from the current directory (./). hope this helps someone avoid scratching his head

Resources