summaryrefslogtreecommitdiff
path: root/app/controllers/tutorials_controller.rb
blob: c133c891a04f3f2c8e84340307823f7836c4fdbd (plain)
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
49
50
51
52
53
class TutorialsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]

  def index
    @tutorials = Tutorial.includes(:tags).page(params[:page]).per(15)
    expires_in(10.minutes)
  end

  def show
    @tutorial = Tutorial.find(params[:id])
    expires_in(24.hours)
  end

  def new
    @tutorial = Tutorial.new
    @user = current_user
  end

  def edit
    @tutorial = current_user.tutorials.find(params[:id])
  end

  def create
    @tutorial = current_user.tutorials.create(tutorial_params)
    current_user.tag(@tutorial, :with => params[:tutorial_tags], :on => :tags)
    if @tutorial.save
      redirect_to dashboard_path, :notice => t(:tutorial_saved)
    else
      flash[:error] = @tutorial.errors.full_messages
      render :new
    end
  end

  def update
    @tutorial = current_user.tutorials.find(params[:id])
    current_user.tag(@tutorial, :with => params[:tutorial_tags], :on => :tags)
    if @tutorial.update_attributes(tutorial_params)
      redirect_to @tutorial
    else
      render :edit
    end
  end

  def destroy
    @tutorial = current_user.tutorials.find(params[:id])
    @tutorial.destroy
    redirect_to dashboard_path
  end

  def tutorial_params
    params.require(:tutorial).permit(:url, :heading)
  end
end