2023-10-28 03:54PM
参考:http://rails_book.siwei.me/part3_rails_premier/interface_document.html
为了更好的保护、维护我们的代码,所以通常将接口文件写在项目的:./controllers/interface/目录下。
1. 创建 app/controller/interface/ 目录
2. 创建 app/controller/interface/api_controller.rb 文件
class Interface::ApiController < ActionController::Base
def all_article
all_article = Article.all.map do |article|
{ :id => article.id,
:title => article.title,
:content => article.content,
:created_at => article.created_at,
:attachment => article.attachment,
}
end
render :json => { :success =>true, :result => all_article}
end
end
3. 接口路由的配置:
在路由中增加下面的内容:
namespace :interface do
resources :api, :only => [] do
collection do
get :all_article
end
end
end
4. 接口的调用:
http://域名/interface/api/all_article
或者:
http://localhost:端口号/interface/api/all_article
5.返回的结果:
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论