Rails 3.1 - Changing Default Scaffold Views and Template

A lot of scaffold with similar layout and controller in rails

In Rails you can customize the default layouts used by the generator. There are several tutorials available online.

You can change the model file, the controller and/or the action templates. As explained in this answer

You can override default view templates by creating your own templates in lib/templates/erb/scaffold folder of your rails app.

lib/templates/erb/scaffold/_form.html.erb
lib/templates/erb/scaffold/edit.html.erb
lib/templates/erb/scaffold/index.html.erb
lib/templates/erb/scaffold/new.html.erb
lib/templates/erb/scaffold/show.html.erb

Easy way to cusomize views of Rails' scaffold generator?

You can find the answer in this RailsCast, note particularly the section on "Customizing Templates". You create the files in your Rails root lib/ directory, so it won't apply to every app, just your current one, and it will be independent of Rails versions.

How can I re-run a scaffold view create from a new template

rails g erb:scaffold post title content

However, you need to specify all attributes in this command.

A shortcut is to use schema_to_scaffold which you could copy a string and append it to rails g erb:scaffold so that you will not need to manually include all attributes one by one

Altering the current outcome of the model of a generated scaffold

It's defined in https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/activerecord/lib/rails/generators/active_record/model/templates/model.rb.tt

You should be able to add it there

Second, configure your options in https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/activerecord/lib/rails/generators/active_record/model/model_generator.rb

Rails 3, find current view while in the layout

In Rails 3.0.3, I was able to see the name of the controller and action using controller_name and action_name. But those are not publicly documented (at least the action name) so I wouldn't depend on it long-term.

It might be better to monkey patch template render. In an initializer:

module ActionView::Rendering 
alias_method :_render_template_original, :_render_template
def _render_template(template, layout = nil, options = {})
@last_template = template
_render_template_original(template, layout, options)
end
end

Then use @last_template in your layout.

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

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.



Related Topics



Leave a reply



Submit