Jun Dalisay
Jun Dalisay

Reputation: 1265

Rails 5 update date using link_to

I have Contract model with an accept_date attribute

I want to update the accept_date via link_to using a custom method which can also change its AASM state. Is it better to do it by passing the params from the view as link_to put or by doing it in the controller as link_to get?

view

// put
= link_to 'Accept', custom_method(@contract, contract: {accept_date: Time.now}), method: :put
// get
= link_to 'Accept', custom_method(@contract)

controller

def custom_method
 @contract.aasm_accept!
 @contract.update(contract_params) # if put
 @contract.update_attributes(accept_date: Time.now) # if get

Also, what is the difference between Time.now and Date.today and other helpers to get the current time and are they dependent on t.date t.time and t.datetime? Can I use Date.today for a t.time attribute?

I used the methods above and the database shows a commit, but nothing is stored.

Upvotes: 0

Views: 122

Answers (1)

Vishal
Vishal

Reputation: 7361

you can directly add method after updating state like below, so need to write in the method of the controller, use PUT instead of GET because you are updating something

your_model.rb

event :aasm_accept do
  transitions from: :nothing, to: :everything, after: proc { |*_args| update_date_value }
end


def update_date_value
 # your business logic
end

The difference of .current and .now is .now use the server's timezone, while .current use what the Rails environment is set to. If it's not set, then .current will be same as .now.

Date.today will give you today's date without timezone. Time.current will give you current time with time zones configured in rails application.

Upvotes: 1

Related Questions