pollinoco
pollinoco

Reputation: 435

Rails: How to add dynamic days or minutes?

In my controller i have

look the commented line, i need dynamic minutes to @presentation.date

def create
    @presentation = Presentation.new(params[:presentation])
    @presentation.user_id = current_user.id
    @category = Category.find_by_id(params[:presentation][:category_id])
    @addtime = params[:presentation][:date]
#####@presentation.date = @category.date + @addtime.minutes ---------------------------- With this line not work 
    @presentation.date = @category.date + 60.minutes
    @presentation.expiration = @category.date + 3.days

    respond_to do |format|
      if @presentation.save
        flash[:notice] = 'Presentation was successfully created.'
        format.html { redirect_to(@presentation) }
        format.xml  { render :xml => @presentation, :status => :created, :location => @presentation }
      else
        @addtime = params[:presentation][:date]
        @category = Category.find_by_id(params[:presentation][:category_id])
        @minutes = Minute.minutes_presentations(params[:presentation][:category_id])
        format.html { render :action => "new" }
        format.xml  { render :xml => @presentation.errors, :status => :unprocessable_entity }
      end
    end
  end

Upvotes: 0

Views: 2318

Answers (2)

NilColor
NilColor

Reputation: 3532

You can use #in available in Rails like this

irb(main)> Time.zone.now
=> Thu, 18 Jan 2024 20:26:35.035644801 UTC +00:00
irb(main)> Time.zone.now.in("10".to_i.minutes)
=> Thu, 18 Jan 2024 20:36:36.377030022 UTC +00:00

where "10" is the string from your parameter. I.e.

@presentation.date = @category.date.in(@addtime.to_i.minutes)

Upvotes: 0

jdl
jdl

Reputation: 17790

minutes isn't a method on String. You need to convert your param to a number first.

@presentation.date = @category.date + @addtime.to_f.minutes

Upvotes: 3

Related Questions