2010-09-03 40 views
0

我是ROR新手API的示例Ruby api代碼 - 博客帖子

我在我的應用程序中有博客的api代碼。這包括創建,顯示,更新功能。

我想在Ruby中爲此編寫示例Api代碼。如何寫這個?

請給出建議。

我創建的代碼是

def create 

@blogpost = Blogpost.new(params[:blogpost]) 
@blogpost.user = current_user 

respond_to do |format| 
    if @blogpost.save 
    check_for_pingbacks(@blogpost) 
    format.html { redirect_to root_path } 
    format.xml { render :xml => @blogpost.to_xml(to_any_options) } 
    format.json { render :json => @blogpost.to_json(to_any_options) } 
    else 
    format.html {redirect_to root_path } 
    format.xml { render :xml => @blogpost.errors, :status => :unprocessable_entity } 
    format.json { render :json => @blogpost.errors, :status => :unprocessable_entity } 
    end 
end 
else 
    respond_to do |format| 
    error=Hash.new 
    error[:error]="Only Post Request is Allowed" 
    format.xml {render :xml=>error.to_xml({:root=>'errors'})} 
    format.json {render :json=>error.to_json({:root=>'errors'})} 
    end 
end 
end 

回答

0

如果我理解正確的話,它看起來像你想爲您的應用程序RDocs? Rails已經內置支持與下列rake任務這樣做:

rake doc:app 

這將在/ DOC /應用/你的應用程序的文檔。

至於寫作RDoc的,有很多資源在那裏,在RDoc文檔本身是一個良好的開端:http://rdoc.sourceforge.net/doc/index.html