文档Rails项目与Yard

时间:2013-10-02 05:22:50

标签: ruby-on-rails documentation yard

我正在使用Yard生成文档,但我找不到有关ruby on rails项目的任何示例文档。我在getting started tutorial上只发现了短rubydoc.info和一些github项目,但它们根本没有记录。请有人告诉我如何正确记录控制器(带动作),模型,轨道项目的路线

例如我有这样的控制器:

class ArticlesController < ApplicationController
  before_filter :authenticate_user!, except: [:show]
  before_filter :restrict_user, only: [:edit, :update]

  def index
    @articles = current_user.articles.sort_by_rating.
        paginate(:page => params[:page],
                 per_page: 5)
  end

  def new
    @user = User.find(params[:user_id])
    @article = @user.articles.build
  end

  def create
    @user = User.find(params[:user_id])
    @article = @user.articles.build(params[:article])

    if @article.save
      redirect_to  @article, notice: 'Article was successfully created.'
    else
      render action: "new"
    end
  end
end

用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable :recoverable
  devise :database_authenticatable, :registerable,
         :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false

  has_many :articles, dependent: :destroy

  letsrate_rater
end

1 个答案:

答案 0 :(得分:-2)

我认为这样的事情会对你有用。但我不能100%确定你的问题是什么。

class ArticlesController < ApplicationController
  before_filter :authenticate_user!, except: [:show]

  def index
    @articles = current_user.articles.sort_by_rating.
        paginate(:page => params[:page],
                 per_page: 5)
  end

  def new
    #I think this is self explanatory. 
    @article = Article.new
  end

  def create
    #devise has the user id with current_user and rails will do the job for you. 
    @article = current_user.articles.build(article_params)

    if @article.save
      redirect_to  @article, notice: 'Article was successfully created.'
    else
      render action: "new"
    end
  end

end

文章模型看起来像这样

class Article < ActiveRecord::Base

  #Remember Rails 4 does use attr_accessible on the model
  attr_accessible :title,  :body, :user_id

  #Article belongs to user
  belongs_to :user

  #Here we can validate presense of these fields.
  validates :title, :body, :user_id, :presence => true


end

用户模型。

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable :recoverable
  devise :database_authenticatable, :registerable,
         :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false

  #relationship with the article
  has_many :articles, dependent: :destroy

  letsrate_rater
end

如果您询问如何记录您的项目,请尝试Yard。 http://yardoc.org/或使用RDoc,您可以使用以下内置Rake任务将其转换为浏览器友好的HTML:

$ rake doc:app
相关问题