我刚刚开始学习Ruby on Rails,这是一个新手问题。
我做了
$ rails generate scaffold Product blah blah...
这是脚手架生成的视图文件的片段。
<td class="list_actions">
<a href="/products/1">Show</a><br/>
<a href="/products/1/edit">Edit</a><br/>
<a href="/products/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
</td>
我的问题是,
为什么Ruby on Rails会以这种方式为“Destory”生成链接?将方法更改为“HTTP DELETE”......以及所有这些。
而不是以更直接的方式做到, 即
<a href="/products/1/delete" data-confirm="Are you sure?">Destroy</a>
答案 0 :(得分:2)
因为默认情况下你有such routes for the resources,默认情况下DELETE方法用于销毁操作,PUT方法用于更新。所以,如果你这样做链接:
<a href="/products/1/delete" data-confirm="Are you sure?">Destroy</a>
指向网址/products/:id/delete
的GET操作,并且只有在routes.rb
文件中定义该路由时才会起作用
答案 1 :(得分:2)
因为它是语义的。
虽然Web浏览器通常只发送HTTP GET和POST请求,但请求的语义意图是它们与CRUD动词一对一映射:
......从实际的角度来看,资源丰富的路线反映了这一点。