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.
Related
I have been struggling to understand why I can't get view files created for my Rails project. The documentation shows that generating a controller also generates an associated view:
Rails Docs
If I use that same command, I get one file generated, being the controller.
My command
The only two files within the Views folder in my Rails project are mailer.html.erb and mailer.text.erb
Am I doing something wrong? Do I manually create the view files, and if so I can't seem to get them "connected" to the associated controller. I am new to Rails, so any insight would be helpful. Thanks!
try this command :
rails g controller Articles index create
You can create methods and views dynamically after If you typed controller name in command.
you are creating only a controller in your command not any methods that's why the views are not generated. If you need to generate views for the particular methods then you may run the below listed command.
rails g controller Articles index show
Here, index, show are the methods name. So, this command will create ArticlesController and also the respective view files.
You can use the scaffold option.
rails g scaffold Post name:string title:string content:text
This command will generate the following files.
File
Purpose
db/migrate/20100207214725_create_posts.rb
Migration to create the posts table in your database (your name will include a different timestamp)
app/models/post.rb
The Post model
test/unit/post_test.rb
Unit testing harness for the posts model
test/fixtures/posts.yml
Sample posts for use in testing
config/routes.rb
Edited to include routing information for posts
app/controllers/posts_controller.rb
The Posts controller
app/views/posts/index.html.erb
A view to display an index of all posts
app/views/posts/edit.html.erb
A view to edit an existing post
app/views/posts/show.html.erb
A view to display a single post
app/views/posts/new.html.erb
A view to create a new post
app/views/posts/_form.html.erb
A partial to control the overall look and feel of the form used in edit and new views
test/functional/posts_controller_test.rb
Functional testing harness for the posts controller
app/helpers/posts_helper.rb
Helper functions to be used from the post views
test/unit/helpers/posts_helper_test.rb
Unit testing harness for the posts helper
app/assets/javascripts/posts.js.coffee
CoffeeScript for the posts controller
app/assets/stylesheets/posts.css.scss
Cascading style sheet for the posts controller
app/assets/stylesheets/scaffolds.css.scss
Cascading style sheet to make the scaffolded views look better
I am new to programming in Ruby and Rails. I am following the instructions to generate a new controller (rails generate controller welcome index). In the terminal, it creates correctly the controller, it appears:
create app/controllers/welcome_controller.rb
/ route get 'welcome/index'
/ invoke erb
/ create app/views/welcome
/ create app/views/welcome/index.html.erb
and the rest.. but I can't see the controller in the text editor.
I tried to delete and generate again the controller, and I have also check with ls in the terminal, and it lists the controller, but in my text editor, I don't see the view and the action in the controller. I am using a ruby 2.3.3 in a mac.
Looks like your editor has cached file tree, you need to refresh it.
What is the editor?
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 am really new to rails and working to customize a project. I generated a new scaffold, called it New_Scaffold. When the New_Scaffold's index.html.erb is displayed, the wrapper of the application.html.erb (where the yield method is called) is not displayed.
I thought it was automatic in rails, is it not ? How can I display the application.html.erb to wrap the New_Scaffold's index.html ?
The scaffold command generates a bunch of things for a model and controller for you automatically.
If you want to try Rails out try to create a new project like this:
rails new blog
cd blog
rails generate scaffold Post title body:text
rake db:migrate
rails server
This will create a basic project with one model, Post, that will have a generic setup with views and a controller that responds to all the RESTful actions.
the application.html.erb file can be found under views/layouts
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.