Class: Blog::BlogsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#can_manage_blogs, #can_manage_contexts

Methods included from BlogsHelper

#allowable_html, #construct_blog_path, #posted_by_on

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/blog/blogs_controller.rb', line 32

def create
  # No, we don't pass these in the form
  params[:blog].merge!(:user_id => current_user.id)
  
  @blog = Blog.new(params[:blog])

  if @blog.save
    redirect_to(construct_blog_path(@blog, :show), :notice => "#{@blog.title} was successfully created.")
  else
    @context_options = Context.selections
    @context = @blog.context
    render :action => :new
  end
end

#destroyObject



73
74
75
76
77
78
79
80
81
# File 'app/controllers/blog/blogs_controller.rb', line 73

def destroy
  title = @blog.title
  @blog.destroy
  
  respond_to do |format|
    format.html { redirect_to construct_blog_path(@blog, :index), :notice => "'#{title}' was successfully removed" }
    format.json { head :ok }
  end
end

#indexObject



10
11
12
13
14
15
16
17
# File 'app/controllers/blog/blogs_controller.rb', line 10

def index
  if @context
    @blogs = Blog.where("context_id = ?", @context.id).limit(10).order('created_at desc').paginate(:page => params[:page]).per_page(10)
    @archive = Blog.where("context_id = ? and id not in (?)", @context, @blogs.collect{|b| b.id}).limit(10).order('created_at desc')
  else
    @blogs = Blog.all
  end
end

#newObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/blog/blogs_controller.rb', line 19

def new
  # If we are at /blogs/new the it will look for a 'blogs' blog context. Likely one does not exist so use the first
  # created as a default
  @context ||= Context.first
  @context_options = Context.selections
  @blog = Blog.new(:context => @context, :photo => Photo.new)

  respond_to do |format|
    format.html { render }
    format.xml  { render :xml => @blog }
  end
end

#showObject

Raises:

  • (ActiveRecord::RecordNotFound)


47
48
49
50
51
52
53
54
55
# File 'app/controllers/blog/blogs_controller.rb', line 47

def show
  raise ActiveRecord::RecordNotFound unless @blog.present?
  @other_blogs = Blog.where("context_id = ? and id != ?", @context, @blog.id).limit(10).order('created_at desc')

  respond_to do |format|
    format.html # show.haml relative to controller calling it
    format.xml  { render :xml => @blog }
  end
end

#updateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/blog/blogs_controller.rb', line 58

def update
  respond_to do |format|
    if @blog.update_attributes(params[:blog])
      format.xml { render :xml => @blog, :status => :created, :location => @blog }

      format.html do
        redirect_to(construct_blog_path(@blog, :show), :notice => "#{@blog.title} was successfully updated.")
      end
    else
      format.html { render :action => :edit }
      format.xml  { render :xml => @blog.errors, :status => :unprocessable_entity }
    end
  end
end