【问题标题】:What is the best way to a get/post action in Rails ? How is resources internally converted?在 Rails 中获取/发布操作的最佳方式是什么?资源是如何内部转换的?
【发布时间】:2011-03-06 20:26:22
【问题描述】:

我想知道在代码和路由方面创建 get/post 操作集标准过程的更好方法是什么。

每当我需要获取页面时,我只需创建操作并添加如下路线:

  scope :path => '/town', :controller => :town do
    match '/' => :index, :as => 'town'
  end

这可以让我获得城镇页面。如果我正确理解 Rails 路由,这可能会将 /town 视为帖子页面,但我没有说任何真正的安全问题。

当我有一个由我的控制器处理的发布操作时,我通常会通过评论启动那里的功能:

# 已发布的操作

因为我想分开获取/发布操作。我添加了与上面类似的路线。我想对于发布操作,我可能会进行匹配:通过发布。我真的很想看看 Rails 如何在内部转换“resources :whatever”,以便为我的路线做同样的事情。

您能否解释一下您是如何创建发布的操作的,以及是否有人也知道“资源:用户”实际转换为什么?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    resources :things 翻译成以下内容:

    Verb    Path             Action (method in ThingsController)
    ----    ----             ------
    GET     /things          index
    GET     /things/new      new
    POST    /things          create
    GET     /things/:id      show
    GET     /things/:id/edit edit
    PUT     /things/:id      update
    DELETE  /things/:id      destroy
    

    可以手动写成

    get    'things'          => 'things#index', :as => :things
    get    'things/new'      => 'things#new',   :as => :new_thing
    post   'things'          => 'things#create'
    get    'things/:id'      => 'things#show',  :as => :thing
    get    'things/:id/edit' => 'things#edit',  :as => :edit_thing
    put    'things/:id'      => 'things#update'
    delete 'things/:id'      => 'things#destroy'
    

    如果你想使用resources减去某些动作,你可以这样做

    resources :things, :except => [:edit, :update]
    

    Rails routing guide 是学习路由系统的绝佳资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      相关资源
      最近更新 更多