Search code examples
ruby-on-railstheory

Why doesn't Rails generate a view file automatically?


Theory question. Say one runs..

rails generate controller Sessions --no-test-framework

why does RAILS create "app/views/sessions" path/folder but no basic view file, like "new.html.erb"? You need a basic view file to get the request test(s) to pass anyway.


Solution

  • If you want to create a view while generating a controller you would have to do this:

    rails generate controller sessions (the name of the view) 
    

    So if you were to want a index view for that controller you would run this command:

    rails generate controller sessions index 
    

    That will create the index.html.erb and the index action in your sessions controller

    The rails generator will create a controller that looks like this:

    class SessionsController < ApplicationController 
      def index
      end
    end