Rails菜鸟求助,关于api接口

2013-08-17 15:59:00 +08:00
 androidBrant
假设我有一个Joke的model和controller,我要做一个接口给移动端调用(返回xml),我想以分页的形式(即请求参数中传过来page和pageCount)返回数据,我在controller中定义了
def getJokeByPageAndPageCount

end
这个方法,后面怎么做,把路由设到这个方法,如何分离出page和pageCount参数。。。


完全的web菜鸟,帮忙,Thank you
3209 次点击
所在节点    程序员
6 条回复
leiz
2013-08-17 16:49:50 +08:00
# route.rb
match '/get_joke' to: 'jokes#getJokeByPageAndPageCount', via: 'get' # or post

# jokes_controller.rb
def getJokeByPageAndPageCount
# do whatever you want
respond_to do |format|
format.xml { # send whatever you want }
end
end
leiz
2013-08-17 16:52:07 +08:00
# jokes_controller.rb

def getJokeByPageAndPageCount
# ...
page = params[:page]
page_count = params[:PageCount]
rails3
2013-08-17 16:53:58 +08:00
@per_page = 10
users = User.paginate(:page => params[:page], :per_page => @per_page)
users_count = User.all.count
@page_count = users_count % @per_page == 0 ? users_count % @per_page : users_count % @per_page + 1
respond_to do |format|
format.html # index.html.erb
format.xml { render xml: {:page_count => @page_count, :users => users}.to_xml }
end
androidBrant
2013-08-17 17:49:09 +08:00
@leiz
@rails3 Great, thank you
androidBrant
2013-08-17 18:38:37 +08:00
@leiz
@rails3

page和page_count如何传参?http://localhost:3000/api/getJokeByPageAndPageCount?page=0 这样不行

我的路由
match '/api/getJokeByPageAndPageCount', to: 'jokes#getJokeByPageAndPageCount', via: 'get'
leiz
2013-08-17 18:52:49 +08:00
@androidBrant
这个问题和rails无关,你先去看看 html form, ajax

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/79385

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX