Search code examples
ruby-on-railsruby-on-rails-4viewcontroller

Generating sessions views in Ruby on Rails?


I ran the command rails generate controller sessions to build my sessions applications file. The sessions_controller.rb and /views/sessions/ folder were created. However, I am not seeing the following files in the sessions folder:

create.html.erb
destroy.html.erb
new.html.erb

Are these files supposed to be auto generated or is it okay to create them manually?


Solution

  • rails g controller sessions
    

    will only create a controller itself and a directory to house the views, no routes or view files.

    To get those files, instead run

    rails g controller sessions create destroy new
    

    Note that when the routes are generated for this, they will not be named routes. They will loo like:

      get 'sessions/create'
      get 'sessions/destroy'
      get 'sessions/new'
    

    So it might be preferable to change those to a resource

    resources :sessions, only: [:create, :new, :destroy] 
    

    which will give you the ability to use those paths, eg,

    new_session_path
    

    Alternatively, you could create you own names for them

      get 'sessions/new', to: 'sessions#create', as: :new