2013-10-02 6 views
4

야드로 문서를 생성하고 있지만 레일즈 프로젝트에서 루비에 대한 예제 문서를 찾을 수 없습니다. 나는 단지 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 
+0

무엇을하려하십니까? – Benjamin

+0

방금 ​​귀하 또는 다른 사람들에게 도움이 될 수있는 질문에 대한 답변을 추가했습니다. – engineersmnky

답변

-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 사용 브라우저 친화적 인 HTML으로 바꿀 수있는 다음과 같은 내장 레이크 작업 :

$ rake doc:app 
+0

당신은 나를 미소 짓는다.) 잘 작동하고,이 컨트롤러와 모델에 대한 문서를 작성하고 싶다. 제대로 쓰는 법을 모르겠다. 어쩌면 문서화를위한 몇 가지 규칙이있다. http : // rubydoc .info/gems/yard/file/docs/GettingStarted.md –

+0

그것이 기쁘다. 내 대답을 받아들이시겠습니까? 마당을 사용하는 법을 묻고 있습니까? – Benjamin

+0

컨트롤러 작업에 대한 문서는 어떻게됩니까? 예, Yard를 사용하는 작업에 param 및 return 태그를 사용하는 방법을 모르겠습니다. –

0

나는이 질문은 오래 알고 있지만 데이터 유형 db/schema.rb에서 가져온와 내가 attr_accessible에 대한 플러그인을 만들었습니다. 이 플러그인은 직접 YARD::Handlers::Ruby::AttributeHandler 떨어져 기반으로합니다

require 'active_support/inflector' 
class AttrAccessibleHandler < YARD::Handlers::Ruby::AttributeHandler 
    include ActiveSupport::Inflector 
    handles method_call(:attr_accessible) 
    namespace_only 


    process do 
    return if statement.type == :var_ref || statement.type == :vcall 
    read, write = true, true 
    params = statement.parameters(false).dup 
    hashify_object_types(underscore(namespace.name).pluralize) 
    # Add all attributes 
    validated_attribute_names(params).each do |name| 
     namespace.attributes[scope][name] ||= SymbolHash[:read => nil, :write => nil] #Comment this out to see getter and setter methods 

     # Show their methods as well 
     {:read => name, :write => "#{name}="}.each do |type, meth| 
      object_type = attribute_type(name) 
      o = MethodObject.new(namespace, meth, scope) 
      if type == :write 
      o.parameters = [['value', nil]] 
      src = "def #{meth}(value)" 
      full_src = "#{src}\n @#{name} = value\nend" 
      doc = "Sets the attribute #{name}\n 
        @param [#{object_type}] value the value to set the attribute #{name} to." 
      else 
      src = "def #{meth}" 
      full_src = "#{src}\n @#{name}\nend" 
      doc = "@return [#{object_type}] #{name}\nReturns the value of attribute #{name}" 
      end 

      o.source ||= full_src 
      o.signature ||= src 
      o.docstring = doc if o.docstring.blank?(false) 
      register(o) 

      # Regsiter the object explicitly 
      namespace.attributes[scope][name][type] = o #Comment this out to see getter and setter methods 
     end 
    end 
    end 
    def hashify_object_types(table) 
    table_section = "" 
    inside = false 
    File.open("db/schema.rb").each do |line| 
     if line =~ /create_table "#{table}"/ || inside == true 
     inside = true 
     table_section << line.chomp 
     break if line.chomp.strip == "end" 
     end 
    end 
    @hash_table = convert_keys(Hash[table_section.scan(/t\.(\w+)\s+"(\w+)"/).map(&:reverse)]) 
    end 
    def attribute_type(name) 
    @hash_table[name] || "Object" 
    end 
    def convert_keys(h) 
    h.each_with_object({}) do |(k,v),obj| 
     obj[k] = v == "datetime" ? "DateTime" : v.capitalize 
    end 
    end 
end 

은이 플러그인은 루트 디렉토리에있는 가정을하고 당신은 db/schema.rb 파일이 있습니다. 또한 테이블 이름이 강조된 복수형 문자열이 아닌 클래스를 처리하지 않습니다. BookAuthor #=> book_authors