Akhshay
Akhshay

Reputation: 15

How can we assign values to certain model variables in ruby on rails forms without the user having to input them?

I have generated a scaffold in rails to generate a model called transactions which has :

from(int) 
to(int) 
amount(double) 
status(string) 
kind(string) 
start(datetime) 
effective(datetime). 

A form to this effect was automatically created. What I want to know is, is there a way to only get some of these values from the user, and add the others automatically? In this case, from, to, amount and kind need to be entered by the user. status should always be defaulted to "pending", and start should have the current date and time. effective should be null.

Upvotes: 0

Views: 165

Answers (3)

Omkar
Omkar

Reputation: 112

You can do this in many ways

First:

You can use Active Record Callbacks to accomplish this.

Add callback in your model app/models/transaction.rb

before_create :assign_default_attributes

def assign_default_attributes
  self.status = 'pending' if self.pending.blank?
  self.start  = Time.now if self.start.blank?
end

Note: Make sure you remove status, start and effective from permitted params from controller.

Second

Modify app/controllers/transactions_controller.rb create action.

def create
  transaction = Transaction.new(status: 'pending', start: Time.now)
  transaction.assign_attributes(transaction_params)
  if transaction.save
    redirect_to transactions_path, notice: 'Transaction Created'
  else
    flash[:alert] = 'Enter Valid data'
    render :new
  end
end

Note: Make sure you remove status, start and effective from permitted params from controller.

Upvotes: 1

Shannon
Shannon

Reputation: 3018

You can add a migration to change the default values:

class ChangeDefaultValueForStatus < ActiveRecord::Migration
  def change
    change_column :transactions, :status, :string, default: "Pending"
    change_column :transactions, :effective, :datetime, default: nil
  end
end

Instead of using start, you can use the in-built timestamps to automatically get the date and time of when a record was created or updated:

class AddTimestampsToTransactions < ActiveRecord::Migration
    def change_table
      add_column(:transactions, :created_at, :datetime)
      add_column(:transactions, :updated_at, :datetime)
    end
end

Upvotes: 1

Milind
Milind

Reputation: 5112

i think you need default values

  1. for status,set the default value.

    Add this in your migration to add default value to columns-

    t.string :status, default: "Pending"

check Migration api for more details

  1. For start date,you can set todays time in your form.

       <div class="form_group">
          <label>Start time:</label></br>
          <%= f.datetime_select :starts_at , :class=>"form-control",:start_year => Date.current.year, :end_year => Date.current.year,:selected=> Date.today, :order=> [:day, :month, :year],:start_year=> Time.now.year,:default=> 1.days.from_now,:prompt=> {day: 'Choose day', month: 'Choose month', year: 'Choose year'},:placeholder=>"Enter start time",:required=>true %>
    
        </div>
    

Check the datetime_select api for more details.

Upvotes: 0

Related Questions