Skip to content

Instantly share code, notes, and snippets.

@krames
Last active August 29, 2015 14:21
Show Gist options
  • Save krames/bfeb735f4247ebd441e1 to your computer and use it in GitHub Desktop.
Save krames/bfeb735f4247ebd441e1 to your computer and use it in GitHub Desktop.
How to generate rails scaffold and edit the resulting views.

Generate scaffold

To create a model called HighScore with a game field of type strubg and a score field of type integer you would do the following:

bundle exec rails generate scaffold HighScore game:string score:integer
    invoke  active_record
    create    db/migrate/20130717151933_create_high_scores.rb
    create    app/models/high_score.rb
    invoke    test_unit
    create      test/models/high_score_test.rb
    create      test/fixtures/high_scores.yml
    invoke  resource_route
     route    resources :high_scores
    invoke  scaffold_controller
    create    app/controllers/high_scores_controller.rb
    invoke    erb
    create      app/views/high_scores
    create      app/views/high_scores/index.html.erb
    create      app/views/high_scores/edit.html.erb
    create      app/views/high_scores/show.html.erb
    create      app/views/high_scores/new.html.erb
    create      app/views/high_scores/_form.html.erb
    invoke    test_unit
    create      test/controllers/high_scores_controller_test.rb
    invoke    helper
    create      app/helpers/high_scores_helper.rb
    invoke    jbuilder
    create      app/views/high_scores/index.json.jbuilder
    create      app/views/high_scores/show.json.jbuilder
    invoke  assets
    invoke    coffee
    create      app/assets/javascripts/high_scores.js.coffee
    invoke    scss
    create      app/assets/stylesheets/high_scores.css.scss
    invoke  scss
   identical    app/assets/stylesheets/scaffolds.css.scss

If you notice it created db/migrate/20130717151933_create_high_scores.rb which contains the database migrations to create your model. To run these type the following:

bundle exec rake db:migrate

After you run the migration, you can then fire up your server using

bundle exec rails server

You should be able to view the generated scaffolding by going to http://localhost:3000/high_score.

Edit Scaffold

To edit the generated pages you would go to the following:

Page File
List of high scores app/views/high_scores/index.html.erb
View single score app/views/high_scores/show.html.erb
New score app/views/high_scores/new.html.erb app/views/high_scores/_form.html.erb
Edit existing score app/views/high_scores/edit.html.erb app/views/high_scores/_form.html.erb

Note that the app/views/high_scores/new.html.erband app/views/high_scores/edit.html.erb include the app/views/high_scores/_form.html.erbpartial (you can tell it is a partial because the file name starts with an _).

You should be able to edit a file while the server is running and then just reload the page in the browser to see the changes.

For more information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment