13

I'd like to be able to use Bootstrap 3 and Sass in my RoR project and have the scaffolding generator output Bootstrap 3 HTML. I'm using Ruby 2 with Rails 4.

Nothing too fancy - mostly just having the forms buttons have the appropriate CSS classes.

I've used the Rails Tutorial Sample App (ver 4) as a base which includes the bootstrap-sass gem - but when I use the generator the HTML does not have the proper bootstrap classes - for instance the buttons don't have the btn btn-default class.

I realize that the scaffolding is behaving as it was designed to, it is a base and is meant to be customized (or replaced) - but it seems like it should not be hard to also have the generated HTML be "Bootstrap Ready"

enter image description here

A related question had an answer where someone mentioned that editing the files in the directory lib/erb/scaffold like edit.html.erb - would override the default templates that Rails uses for scaffolding. I'm not opposed to that but I was hoping that there might be a something like a gem that already did this.

I like using the bootstrap-sass gem and I hope that there is a solution that would be compatible with it - I'd rather use scss than less

Seems like there should be several gems to do this.

hichris123
  • 10,145
  • 15
  • 56
  • 70
cwd
  • 53,018
  • 53
  • 161
  • 198
  • So the problem is what, exactly? That your templates don't have the classes applied to them that you want? – cimmanon Feb 05 '14 at 21:09
  • yes - I want the generated template to have the default bootstrap classes for the elements that they contain ( buttons, tables, forms, etc ) – cwd Feb 05 '14 at 21:11
  • if u are using simple_form, u can customize it to add default elements. like config.button_class = 'btn btn-primary'. Also, there is no official release of simple form supporting BS3 – Prasad Surase Feb 07 '14 at 05:04

3 Answers3

24

I've had the same trouble but finally found this: https://github.com/decioferreira/bootstrap-generators

It includes Bootstrap 3.1 and provides scaffolding and you can choose haml and scss as well as other options.

For example when I did rails g scaffold Link guid:string profile:string media_url:string

It automatically produced this:

automatically scaffolded with Bootstrap 3

EDIT FOR HEROKU USERS

I did have trouble pushing my app that is using bootstrap-generators (v3.1.1) to Heroku. Heroku was giving the error File to import not found or unreadable: bootstrap.scss

The fix turned out to be to modify the automatically generated bootstrap-generators.scss file. Change @import "bootstrap.scss"; to @import "bootstrap"; (eg just remove the extension).

NEW EDIT FOR HEROKU USERS The new gem 3.1.1.1 fixes the bug. You no longer need to change @import "bootstrap.scss"; to @import "bootstrap"; in the bootstrap-geneerators.scss file.

jpw
  • 18,697
  • 25
  • 111
  • 187
  • very nice. i also found [bootstrap-sass-extras](https://github.com/doabit/bootstrap-sass-extras) recently but I think yours looks better. – cwd Feb 14 '14 at 06:06
  • i tried that one too, but this one seems to be a more general solution – jpw Feb 14 '14 at 06:32
  • Strange, that this answer (and question) was not upvoted more. – Alexander Popov Jul 15 '14 at 05:52
  • I have followed your advice and used bootstrap-generators successfully. Even after adding the [Bootswatch CDN](http://www.bootstrapcdn.com/#bootswatch_tab) things were smooth. However I noticed that if I create a scaffold the resulting form uses the standard fields and buttons and not the bootstrap ones. Do you know how can i fix this so those form are automatically generated with the bootstrap formatting? – Cristi Mar 16 '15 at 06:47
5

If you need to customize your generated views more you can actually override the default views with your own.

Just put them in lib/templates/{erb|haml}/scaffold. You can see some example files here.

Nikos
  • 1,052
  • 5
  • 7
1

This may be what you're looking for http://railscasts.com/episodes/328-twitter-bootstrap-basics

bridiver
  • 1,694
  • 12
  • 13
  • Here is the link to the actual gem https://github.com/seyhunak/twitter-bootstrap-rails – bridiver Feb 08 '14 at 16:12
  • 1
    that won't help you, you said you want bootstrap 3 and afaik twitter-boostrap-rails is still on Bootstrap v2.X – jpw Feb 14 '14 at 03:13