Home > database >  Generating sessions views in Ruby on Rails?
Generating sessions views in Ruby on Rails?

Time:09-22

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?

CodePudding user response:

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

CodePudding user response:

you should use command like :- rails generate controller sessions create new destroy it will create following files

      create  app/controllers/sessions_controller.rb
       route  get 'sessions/create'
              get 'sessions/new'
              get 'sessions/destroy'
      invoke  erb
      create    app/views/sessions
      create    app/views/sessions/create.html.erb
      create    app/views/sessions/new.html.erb
      create    app/views/sessions/destroy.html.erb
      invoke  test_unit
      create    test/controllers/sessions_controller_test.rb
      invoke  helper
      create    app/helpers/sessions_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/sessions.scss
  • Related