路线.rb
root :to => 'articles#index'
微贴模型:
class Micropost < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 100 }
end
文章型号:
class Article < ActiveRecord::Base
attr_accessible :content, :title
validates :title, :presence => true,
:length => { :minimum => 2 }
belongs_to :user
end
以及articles_controller.rb 的索引define_method
def index
@articles = Article.paginate(:page => params[:page],
:per_page => 5,
:order => 'created_at DESC')
respond_to do |format|
format.html
format.json { render json: @articles }
end
end
虽然我不怎么写我的articles_controller#index 和索引视图。