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
Related
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
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
I have this namespace:
namespace :manage do
# Directs /manage/products/* to Manage::ProductsController
resources :instructors
end
and I want to generate views in the appropriate sub folder and the controller.
What are the commands?
Just create the controller like this:
$ rails generate controller manage/instructors
create app/controllers/manage/instructors_controller.rb
invoke erb
create app/views/manage/instructors
invoke test_unit
create test/functional/manage/instructors_controller_test.rb
invoke helper
create app/helpers/manage/instructors_helper.rb
invoke test_unit
create test/unit/helpers/manage/instructors_helper_test.rb
As you can see, Rails has created the views folder for you as well. The views itself you need to create in that folder.
(I used Rails 3.0 in this example, but it holds for older and newer versions as well.)
I agree with #rdvdijk, but he left out an important note: controller actions can be appended to the end of this command. For example:
rails generate controller manage/instructors home an_action another_action .. etc
And it will generate views for each controller action specified.
I've created a generator for a controller in rails 3.
Now I want to use this generator as the default generator when using the scaffolding generator.
Is that possible?
The correct position for your customized controller file is lib/templates/rails/scaffold_controller/controller.rb
If you simply want to use your own controller template, you can just put it in lib/templates/rails/scaffold_controller/controller.rb
If you want to replace the scaffold_controller_generator code itself, for example, so that the controller scaffold generates additional class files. you can create lib/generators/rails/my_scaffold_controller/my_scaffold_controller_generator.rb with templates under lib/generators/rails/my_scaffold_controller/templates.
Remember to point rails at your new scaffold_controller in config/application.rb:
config.generators do |g|
g.scaffold_controller = "my_scaffold_controller"
end
For my_scaffold_controller_generator.rb you could copy from the railties gem under railties-3.x.x/lib/rails/generators/rails/scaffold_controller if you want to modify default behaviour, or inherit from it if you just want to add functionality:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class MyScaffoldControllerGenerator < ScaffoldControllerGenerator
source_root File.expand_path("../templates", __FILE__)
def new_funtionality
end
end
end
end
You can override the templates that Rails uses for its generators. In this instance, just place the file at lib/templates/scaffold_controller/controller.rb and modify it how you wish. The next time you run rails g scaffold [modelName] it will pick up this new controller template and use it.
This is covered in Section 6 of the Creating and Customizing Rails Generators official guide.
This seems to have changed slightly with Rails 4. You can see which template the generator will look for in the invoke line when the scaffold is generated, and your template folder name should match this:
rails generate scaffold blub
...
invoke responders_controller
If you're using rails g scaffold_controller blubs the location of the template should be:
lib/templates/rails/scaffold_controller/controller.rb
If you're using rails g scaffold blub the location of the template should be:
lib/templates/rails/responders_controller/controller.rb
If anyone is wondering why this isn't working in a default Rails 4 install, it's because jbuilder is inserting itself into the template path before the override location. I don't need jbuilder so I removed it, but I also reported an issue in Github. Hopefully it'll be fixed soon.
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