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.
Related
I've seen some Rails examples where generating a controller also creates the view file (not just the view folder).
In section 6.4 of Rails Guide, it shows an empty view folder and no view file. This is what my local installation is doing. I don't get any view files.
Is there some way to have Rails auto generate the view file when running rails g controller ...? Or, is it more likely the person created the view file manually and didn't show that part?
Use rails generate scaffold instead which will generate the model, view and controller files for you in a single operation.
If you want to create the models, views, and controllers for a new
resource in a single operation, scaffolding is the tool for the job.
e.g.:
rails g scaffold Post name:string title:string content:text
But, if you really want to use rails g controller and also create the view files for you, then you have to specify the action names for your controller:
rails g controller Controllername index show edit update
This way, it will create the view files for these four actions: app/views/.../index.html.erb, app/views/.../edit.html.erb . . . etc.
But, looking at your case, you should use scaffolding as it will do a lot of work for you.
To generate basic view and controller actions you should run e.g.: rails g controller Controllername index show
Basic view for index and show action will be created.
I've got the same problem if i create an api-project with RubyMine (New Project > Rails Api Project). The first project was created on cli (rails new), ze second in RM:
C:\Projekte\railstest3>ruby bin/rails generate controller Welcome index
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
C:\Projekte\railstest3>cd..
C:\Projekte>cd railstest
C:\Projekte\railstest>ruby bin/rails generate controller Welcome4 index
create app/controllers/welcome4_controller.rb
route get 'welcome4/index'
invoke test_unit
create test/controllers/welcome4_controller_test.rb
C:\Projekte\railstest>rails -v
Rails 5.0.2
Solution is to create new "Application Projects" and not new "Api Project" in RM.
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
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
For example, I created this post_controller earlier in the project but I decide to destroy it later.
Then I use
rails destroy controller post_controller
to remove the controller. But I also want to remove the views that attach to this controller.
In this case, they are the erb files in the app/views/post folder. How can I remove those erb files?
You can run rails d scaffold post but be warned this will destroy the rest of the scaffolded elements including any models/routes/assets etc. Here is the full list of what it will destroy:
⌘ ~/testapp/ rails d scaffold post
invoke active_record
remove migration.rb
remove app/models/post_controller.rb
invoke test_unit
remove test/unit/post_controller_test.rb
remove test/fixtures/post_controllers.yml
invoke resource_route
route resources :post_controllers
invoke scaffold_controller
remove app/controllers/post_controllers_controller.rb
invoke erb
remove app/views/post_controllers
remove app/views/post_controllers/index.html.erb
remove app/views/post_controllers/edit.html.erb
remove app/views/post_controllers/show.html.erb
remove app/views/post_controllers/new.html.erb
remove app/views/post_controllers/_form.html.erb
invoke test_unit
remove test/functional/post_controllers_controller_test.rb
invoke helper
remove app/helpers/post_controllers_helper.rb
invoke test_unit
remove test/unit/helpers/post_controllers_helper_test.rb
invoke assets
invoke coffee
remove app/assets/javascripts/post_controllers.js.coffee
invoke scss
remove app/assets/stylesheets/post_controllers.css.scss
invoke scss
Edit:
To clarify, rails d is just shorthand for rails destroy. It can also be used as rails generate/ rails g.