Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have Ruby on Rails output Bootstrap v3 scaffolding?

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.

like image 891
cwd Avatar asked Feb 05 '14 20:02

cwd


Video Answer


2 Answers

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.

like image 86
jpw Avatar answered Sep 20 '22 15:09

jpw


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.

like image 28
Nikos Avatar answered Sep 19 '22 15:09

Nikos