Rails Controllers

Continued from Rails 101

Opening up app/controllers/posts_controller.rb shows;

  # GET /posts
  # GET /posts.xml
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end

Post.all gets all posts which have been saved to the DB, and stored in the @posts variable. All variables within a ‘def’ (method) are available to the view

Leave a Reply