Frage

I need to store a date in variable: created_date + 1 year. Variable is called decided_at and is date type. I wrote this code in my controller:

def create
  @post = current_user.posts.new(post_params)
  @post.statut = 1 
  @post.decided_at = @post.created_at + 1.year // here is the problem
  if @post.save
    redirect_to @post
  else
    render 'new'
  end
end

It return: undefined method '+' for nil:NilClass

How to repair this?

War es hilfreich?

Lösung 2

Whenever a new method is called, it will initialize to nil.

Try Time.current + 1.year

Andere Tipps

You're building a new post in your controller - but not saving it, therefore it's created_at is not yet set.

I'm not sure why you want to add a year to it, but I can suggest either saving it before trying to update it, or perhaps just setting it to the current time + 1 year.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top