Rails 新手问个 routes 的问题

metal · 2011年12月25日 · 最后由 hechian 回复于 2012年02月04日 · 2946 次阅读
resources :categories do
  member do
    post  :destroy
  end
  collection do
     get  :delete
  end
end

我试了很多次,post 这个 action 必须放在 member do 里面 collection do 只能放 get。 rails 是这样的约定吗?

哇,为啥要举个反 RESTful 的例子,已经搞不明白你的意图了

destroy 已经包含在 resources 默认的 7 动作里面了。collention 和 member 都对 http 动作没有限定。

很多的事实证明,如果你不按照规则来,那你就注定悲剧

#1 楼 @Rei 我是重写了 resources :categories 的 7 个默认的,变成了 8 个,我添加了一个 delete 的 action,这个 acition 是通过表单去删除。就像 new 和 create 之间的关系。

我把 post :destroy 加到 collection 里,我一点 delete 它就给我建立 categories,我郁闷,于是看了一下别人的写法,发现 post 写到 member 里,和 get 分开的。

于是我想问一下是不是 rails 的约定?

我如果不把 destroy 改成 post 的话,我点删除会出现 routes 错误。

#3 楼 @metal 首先你的问题不清除,你先把你期望的 URL 地址写出来。 比如:

METHOD   URL                     ACTION
GET          /posts                  index
GET          /posts/:id             show
GET          /posts/:id/edit      edit
PUT          /posts/:id             update
DELETE    /posts/:id             destroy

#4 楼 @huacnlee

category POST   /categories/:id(.:format)       {:action=>"destroy", :controller=>"categories"}
delete_categories GET    /categories/delete(.:format)    {:action=>"delete", :controller=>"categories"}
categories GET    /categories(.:format)           {:action=>"index", :controller=>"categories"}
POST   /categories(.:format)           {:action=>"create", :controller=>"categories"}
new_category GET    /categories/new(.:format)       {:action=>"new", :controller=>"categories"}
edit_category GET    /categories/:id/edit(.:format)  {:action=>"edit", :controller=>"categories"}
PUT    /categories/:id(.:format)       {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format)       {:action=>"destroy", :controller=>"categories"}

这就是我现在的了。其实问题我解决了。

就是没解决之前,我遇到了我在http://localhost:3000/categories/delete?id=1routes给出的错误。这个里面我点删除会出现 大概是说我没有命名 routes。

控制器:

# 删除 ============================================================================
    def delete
        @category = Category.find(params[:id])
        @title = "删除分类"
    end

# 删除 ============================================================================
  def destroy
  @category = Category.find(params[:id])
    if @category.destroy
        redirect_to @category
    end
end
<%= form_for(:category, :url => { :action => "destroy", :id => @category.id }) do |f| %>
<div class="alert-message block-message error">
    <h2>
        你确定要删除 <%= @category.name %> 吗?
    </h2><br />
    <ul>
        <li><%= @category.name %></li>
        <li><%= @category.meta_desc %></li>
    </ul>
    <br />
    <%= submit_tag "删除!", :class => 'btn danger' %> <%= link_to '返回', { :action => 'index' }, :class => 'btn' %>
</div>
<% end %>

于是我加上了 post :destroy,但是我点删除就给我出现了添加分类成功。

resources :categories do
    collection do
       get  :delete
       post  :destroy
    end
  end

最后我改成了这样,终于正常了。

resources :categories do
  member do
    post  :destroy
  end
  collection do
     get  :delete
  end
end

delete 这个 URL 的写法就是有问题的,你不应该写再 collection 里面,而应该写在 member 里面 这样 URL 才会是 /categories/:id/delete 而不是 /categories/delete?id=:id 此外,你的编辑器 tab 缩进有很大问题!!!

#6 楼 @huacnlee 我现在大概了解了 collection 和 member 了。3Q。

总结:collection 是针对 resources 集合,而 member 是针对单数 resource 的操作

NO。member 也可以用在 resources 下。你用 scaffold 生成一個 RESOURCES 然後看 ROUTE 就知道。它們的區別在於 你想一次處理多少 RESOURCE。如果是 SHOW EDIT 這種只處理一個。那麼就是 MEMBER。如果 index bulk_edit 這樣一次操作多個的 那麼就是 COLLECTION。這個字面上理解就比較容易了。member & collection.

分辨 collection & member 的方法很簡單 如果,你是要針對一個集合 (也就是一大堆資料,ex: articles, posts, girls) 去做動作的話 (ex: login),那就是寫在 collection 中 如果是要針對一個集合中的某個個體,像是一篇文章、一個使用者,那就是用 member

GET /articles/by_me #=> collection PUT /rooms/1/join #=> member

所以,若你要刪除的話,應該是 GET /categories/1/delete 這樣才對,所以應該是 member

需要 登录 后方可回复, 如果你还没有账号请 注册新账号