2007-08-01
Rails宝典之第三十五式: 自定义REST动作
关键字: Rails custom REST action
REST将我们的controller限制到7个actions(index/show/new/create/edit/update/desctroy)
这次我们就来看看怎样添加自定义的actions
我们创建了两个方法,我们需要修改routes.rb:
现在我们可以访问http://localhost:3000/tasks;completed了
我们来看看页面中怎样写该链接:
这次我们就来看看怎样添加自定义的actions
class TasksController < ApplicationController
def idnex...
def show...
def new...
def create...
def edit...
def update...
def destroy...
def complete
@task = Task.find(params[:id])
@task.update_attribute :completed_at, Time.now
flash[:notice] = "marked task as complete"
redirect_to completed_tasks_path
end
def completed
@tasks = Task.find(:all, :conditions => 'completed_at IS NOT NULL')
end
end
我们创建了两个方法,我们需要修改routes.rb:
map.resources :tasks, :collection => { :completed => :get }, :member => { :complete => :put}
现在我们可以访问http://localhost:3000/tasks;completed了
我们来看看页面中怎样写该链接:
<%= link_to "Mark as complete", complete_task_path(task), :method => :put %> <%= link_to "Completed Tasks", completed_tasks_path %>
评论
ziyoujiedao
2008-01-24
OK,明白,谢谢hideto
hideto
2008-01-23
不是不可以带参数,而是不带id,但可以带别的参数啊
close_movie_infos_path(:a => 1, :b => 2)生成的url -》/movie_infos/close?a=1&b=2 close_movie_info_path(:id => @movie_info, :a => 1, :b => 2)生成的url -》/movie_info/1/close?a=1&b=2
ziyoujiedao
2008-01-23
哦,大概明白了,那写在collection中的话,close_movie_infos_path不可以带参数了吧 例如close_movie_infos_path(movie_info)
hideto
2008-01-22
用
因为你是定义在:collection中而不是:member中,前者生成的routes为复数,后者则为单数
rake routes看一下生成的named routes的方法名就知道了,应该为close_movie_infos_path
因为你是定义在:collection中而不是:member中,前者生成的routes为复数,后者则为单数
ziyoujiedao
2008-01-22
多个自定义action用GET方式请求,怎么写?
:collection => { :query => :get,:close => :get}可以这样写吗?
map.resources :movie_infos, :collection => { :close => :get}这样时,close_movie_info_path这样写出错
:collection => { :query => :get,:close => :get}可以这样写吗?
map.resources :movie_infos, :collection => { :close => :get}这样时,close_movie_info_path这样写出错
发表评论
- 浏览: 681921 次
- 性别:

- 来自: BJ

- 详细资料
搜索本博客
我的相册
screenshot
共 1 张
共 1 张
最近加入圈子
最新评论
-
Mnesia用户手册:三,构建 ...
要想创建disc_copies和disc_only_copies类型的表有两个前 ...
-- by hideto -
翻译www.djangobook.com之 ...
有个问题问一下: 我先配置了一个urlpatterns是这样的: r'^myd ...
-- by lyhapple -
Why OO sucks
gigix 写道lyl0035 写道为啥就没人想想,其实在面向对象的代码中也流露 ...
-- by hurd -
Why OO sucks
貌似又回到当年java vs c的年代。两种方式,不管是OO还是FP,仅是人处理 ...
-- by python -
大家可以抛弃Java踹死Djan ...
to phoenixup:1,你还别说,你举的什么Struts,Tapestry ...
-- by hideto






评论排行榜