Terminal does not generate controller in the editor - ruby-on-rails

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?

Related

Rails generate controller command not building view files

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

Ruby On Rails Not Creating The View For A Controller

I'm definitely very new to Ruby on Rails. I created a new rails folder like this:
Edit: Sorry for the inconvienience, but I did use the --api tag.
rails new <project-name> -d mysql --api
since I use mysql instead of sqlite. Then, in the tutorial they used a command like this:
rails generate controller Welcome index
I did the exact same thing, however in the log it shows this:
create app/controllers/welcome_controller.rb
route get 'welcome/index'
It didn't seem to create the view. I don't know why it's doing this. After I start the server with rails server I can go to the localhost:3000/welcome/index but it just takes me back to the home page, and in the log it says Started GET .... No Content ... . So it couldn't find the view. How can I fix this?
Unless you created an API only project with the --api flag, the rails generate controller Welcome index command should have created an index.html.erb file in the views folder.
You can either delete the welcome_controller.rb file and the generated line from the route.rb file and run the command again or you can manually create the view with the touch command (assuming your OS supports the touch command):
touch app/views/welcome/index.html.erb
or through you favourite text editor or IDE. On a side note, controller names should be plural (WelcomePagesController for example).

Why doesn't "g controller" also create view file?

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.

Wrapping with application.html - rails

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

generate controller empty app view folder

This is just occurring with me or this new rails when i generate a new controller the view folder still empty.
Try adding a method to the generate command
Rails generate controller yourcontroller index
I found this will make the controller and the view
Views aren't created until you generate them through one of the rails generate commands, or manually by adding in files.
http://guides.rubyonrails.org/getting_started.html

Resources