Rails: Modifying a Model Generated by Scaffolding

How to Edit Rails Scaffold Model generator

Here is the Activerecord template. You need to put it in lib/templates/active_record/model/model.rb as

 ~/D/p/p/generator_test> tree lib/
lib/
├── assets
├── tasks
└── templates #<========
└── active_record
└── model
└── model.rb

Here is my custom template

<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>

#custom method start
before_save :my_custom_method

# my method
def my_custom_method

end
#custom method end

<% attributes.select(&:reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
has_secure_password
<% end -%>
end
<% end -%>

Running scaffold

rails g scaffold property

File created

class Property < ApplicationRecord

before_save :my_custom_method

# my method
def my_custom_method

end

end

Rails: Modifying a Model Generated by Scaffolding

Rails 3 and above use the following code :

rails generate migration add_fieldname_id_to_tablename fieldname:string

Rails 2

ruby script/generate migration add_fieldname_to_tablename fieldname:string 

This no longer works and returns the following error in Rails 3:

ruby: No such file or directory -- script/generate (LoadError)

How do I modify a model generated by scaffolding using the new syntax in Ruby on Rails 3?

Use rails generate migration/model/scaffold.

Using your example: rails generate migration add_fieldname_to_tablename fieldname:string

How to update created_by field in scaffolding generated code?

You need to add users reference to M model and add associations. created_by is not the best name for it. Let imagine that M is abbreviation for Music. In this case you need to create a migration

add_reference :musics, :user

Add to the Music model

belongs_to :user

And to the User model

has_many :musics

And change in the controller

def new
@music = current_user.musics.new
end

def create
@music = current_user.musics.new(M_params)

respond_to do |format|
if @music.save
format.html { redirect_to @music, notice: 'Music was successfully created.' }
format.json { render :show, status: :created, location: @music }
else
format.html { render :new }
format.json { render json: @music.errors, status: :unprocessable_entity }
end
end
end

Is there a way to add fields to an already generated Scaffold in Rails (migration, controller changes, view changes)?

The idea of scaffolds is to get you up and running quickly, but once you start editing the files you probably don't want to be re-scaffolding things.

That said, adding new fields is usually a 3 step process, add the field to the database, add the field to the form, add the field to the permitted list of fields in the controller. Here's what this looks like:

Add the field to the database
From the terminal, type in rails generate migration AddNAMEToMODELS NAME:TYPE. This will add a field named NAME to the MODELS model and its type with be TYPE. This will create the migration file in db/migrations/TIMESTAMP_AddNAMEToMODEL.rb. A real example of this would be rails generate migration AddBirthdateToUsers birthday:date

Add the field to the form
To be able to edit the new field, you will want to add it the the form, which is located at app/views/MODELS/_form.html.erb. The default format for this new field would be:

<div class="field">
<%= form.label :name %>
<%= form.FIELD_TYPE :name %>
</div>

Add field to permitted list in controller
Strong Parameters is a feature that ensures user cannot submit arbitrary data. It allows you to have a white list of permitted fields. This is usually done in a MODEL_params method in you controller. All you need to do in most cases is add the field to the list:

Before:

def category_params
params.require(:user).permit(:email)
end

After:

def category_params
params.require(: user).permit(:email, :name)
end

Ruby on rails, changing recently created scaffold

You can create a migration to rename the attribute in your model, like:

$ rails g migration rename_index_to_username

Inside the migration file, specify the model which to update as the first argument for rename_column, then the old attribute name and the new one:

class RenameIndexToUsername < ActiveRecord::Migration[5.1]
def change
rename_column :users, :index, :username
end
end

Then run rails db:migrate to persis the changes.

After that the error will persist, because there is still some references to index in other files:

  • First edit the user_params created by Rails, replacing index with username.
  • Replace in the show and index page, any reference to the index attribute to username.
  • Replace in the form the text_field helper pointing to index with username.
  • Finally replace also the validation in the model to validates :username ...

Edit a Rails scaffold class to add a new field

You can create a migration which adds a column:

rails generate migration add_my_column_to_fattura my_column:string
rake db:migrate

Rails Modify Scaffold Form

The form is in an automatically generated partial, located in app/views/timesheets/_form.html.erb.

Rails Scaffold based on existing model not working

After further investigation, I think the problem is specific to my Rails installation, not sure what but if you ever get this problem, this is the workaround that I found but requires twitter bootstrap:

rails generate scaffold_controller <YOUR_MODEL_NAME>
rails g bootstrap:themed <YOUR_MODEL_NAME> -f

Hope it helps.

rails scaffold form, simplest way of adding a field?

The scaffold just generates minimalist model, view and controller code along with (usually) a database migration.

If you haven't made any serious changes to any of that code, it may be easier to rerun the scaffold generator. If you haven't run the database migrations, or you haven't committed them, you can add your attributes in the existing migration code and follow up with corresponding changes in the view.

If you've committed the code and someone is already depending on the existing model, you'll want to generate a database change migration to preserve a graceful upgrade/downgrade path.

The scaffold isn't magic, it's just a quick and dirty way of generating code that you can edit.



Related Topics



Leave a reply



Submit