controller

Ruby code posted
created at 24 Aug 14:29

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class GrowsController < ApplicationController

  before_filter :authenticate_user!

  def index
    @grows = current_user.grows
    if @grows.size < 1
      flash[:notice] = "Please add a new garden before continuing."
      redirect_to new_grow_path
    end
  end

  def show
    @grow = Grow.where(id: params[:id]).eager_load(:notifications, :schedules).first
  end

  def new
    @grow = Grow.new
  end

  def update
    @grow=Grow.find(params[:id])
    if @grow.update(grows_attr)
      redirect_to grow_path(@grow, anchor: "four"), notice: 'Parameters updated.'
    else
      redirect_to @grow, error: 'Error occurred while saving data.'
    end
  end

  def create
    @grow = Grow.new(grows_attr)
    @grow.crop = DateTime.now + params[:grow_weeks].to_i.weeks
    @grow.user = current_user
    if @grow.save
      flash[:notice] = "Garden saved successfully."
      redirect_to @grow
    else
      flash[:alert] = "Error encountered while saving."
      render "new"
    end
  end

  def destroy
    Grow.find(params[:id]).delete
    flash[:notice] = "Garden removed successfully."
    redirect_to grows_path
  end
end
1.13 KB in 3 ms with coderay