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
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
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
New to Ruby on Rails so this may be a stupid question. I have an app and I can bundle my gems without issue. So now I want to add some mostly static pages. I try to generate a controller for them with rails generate controller MostlyStatic page1 page2. This should generate a controller named mostly_static and pages named page1 and page2. Instead, I throw an error. Apparently the generate command is trying to connect to the database, which I have not yet created. There is nothing in these pages that should be a database table, so I'm a bit confused as to why the database is being brought into the process at this juncture. I've looked through various tutorials and none say that a database is required to generate controllers for static pages. So... what am I missing? Do I need to create the database first just to generate static pages? And, if so, will subsequently dropping any tables created by that generation impair the function of my app? I really don't want a bunch of useless tables for static pages hanging around. Is there a way to generate these pages and controllers without the database?
You are not following the convention for generating controllers. Generating a controller will not create a database table. You have to do that by calling rails generate model, rails generate resource or rails generate scaffold.
So you want a controller for a few static pages. Try this
rails generate controller static_pages home help contact
Notice the generator is plural and snake case (static_pages). this will generate the static controller and the home.html.erb, help.html.erb, and contact.html.erb pages
Now you can access the pages with these actions in the controller
def home
end
def help
end
def contact
end
Also need to make sure the routes are set up
# routes.rb
match '/home', to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
No database is set up and you can visit the pages. Thats all you need to do. just follow the conventions,like plural controllers and singular models and rails takes care of the details. Hope this gets you started
UPDATE
in response to the comments here is the standard output of generating a controller. Note my example used haml instead of erb, but there is nothing related to the database in the output.
rails g controller static_pages home help contact
create app/controllers/static_pages_controller.rb
route get "static_pages/contact"
route get "static_pages/help"
route get "static_pages/home"
invoke haml
create app/views/static_pages
create app/views/static_pages/home.html.haml
create app/views/static_pages/help.html.haml
create app/views/static_pages/contact.html.haml
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
create spec/views/static_pages
create spec/views/static_pages/home.html.haml_spec.rb
create spec/views/static_pages/help.html.haml_spec.rb
create spec/views/static_pages/contact.html.haml_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
create spec/helpers/static_pages_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
For anyone stumbling across this question, the correct answer is that the database need not exist, but it must be properly configured as if it did exist in the config file. Generating the controller does not actually create the database.
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.