Class: Almanac::BlogsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/almanac/blogs_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#set_blog, #set_current_author

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/almanac/blogs_controller.rb', line 24

def create
  @blog = (Blog.first.nil?) ? Blog.new(params[:blog]) : Blog.first
  @blog.author_id = current_user.id

  respond_with(@blog) do |format|
    if Blog.first.nil?
      if @blog.save
        format.html { redirect_to :root, :notice => 'Blog was successfully created.' }
      else
        format.html { render :action => "new", :alert => 'Something went wrong, try again.' }
      end
    else
      format.html { redirect_to :root, :notice => 'You can only have one blog.' }
    end
  end
end

#editObject



41
42
43
44
45
46
47
# File 'app/controllers/almanac/blogs_controller.rb', line 41

def edit
  @blog = Blog.first

  respond_to do |format|
    format.html
  end
end

#newObject



12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/almanac/blogs_controller.rb', line 12

def new
  @blog = (Blog.first.nil?) ? Blog.new : Blog.first

  respond_with(@blog) do |format|
    if @blog.id
      format.html { redirect_to :root, :notice => 'You can only have one blog.' }
    else
      format.html
    end
  end
end

#updateObject



49
50
51
52
53
54
55
56
57
# File 'app/controllers/almanac/blogs_controller.rb', line 49

def update
  respond_with(@blog) do |format|
    if @blog.update_attributes(params[:blog])
      format.html { redirect_to :root, :notice => 'Blog was successfully updated.' }
    else
      format.html { render :action => "edit" }
    end
  end
end