【问题标题】:how to fix the following error : undefined method `PUT' for #<ActionDispatch::Routing:: Mapper:0x3f8ee48> in rails 3.2?如何修复以下错误:rails 3.2 中#<ActionDispatch::Routing:: Mapper:0x3f8ee48> 的未定义方法“PUT”?
【发布时间】:2014-03-22 15:01:41
【问题描述】:

我目前正在尝试为我的 rails 应用程序创建一个购物车,并且一旦填充我的购物车应该保存在会话中。

每次我尝试运行服务器来测试是否正常工作时,似乎我的路由根本不工作......

请参阅下面我的 routes.rb、我的购物车控制器和我的索引。

任何帮助将不胜感激。

routes.rb

    Sewingsupplies::Application.routes.draw do
  get "cart/index"

  get "cart/success"

  PUT "/cart/:id" => "cart#add"
  DELETE "/cart/:id" => "cart#remove"
  DELETE "/cart" => "cart#clear"
  POST "/cart/checkout"

  devise_for :admins

  resources :catalogues


  devise_for :users

购物车控制器

      def index
        sessions[:cart] = {}
        render 'cart/index'
      end

      def success
      end


      def add

        @catalogue = Catalogue.find(params[:id])
        cart = sessions[:cart]
        cart = {:catalogue => @catalogue.name,category => @catalogue.category, :code => @catalogue.code , :colour=> @catalogue.colour, :description => @catalogue.description, :image  => @catalogue.image , :unitprice => += @catalogue.unitprice, :unitquantity => +=1, :unitweight => += @catalogue.unitweight }
        sessions[:cart] = cart
        render 'cart/add'
      end
end

还有我的索引页

<h1>Listing catalogues</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Code</th>
    <th>Category</th>
    <th>Description</th>
    <th>Unitprice</th>
    <th>Unitquantity</th>
    <th>Unitweight</th>
    <th>Colour</th>
    <th>Image</th>
    <th>User</th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @catalogues.each do |catalogue| %>
  <tr>
    <td><%= catalogue.name %></td>
    <td><%= catalogue.code %></td>
    <td><%= catalogue.category %></td>
    <td><%= catalogue.description %></td>
    <td><%= catalogue.unitprice %></td>
    <td><%= catalogue.unitquantity %></td>
    <td><%= catalogue.unitweight %></td>
    <td><%= catalogue.colour %></td>
    <td><%= image_tag(catalogue.image, :width => 150) if catalogue.image.present?%></td>
    <td><%= catalogue.user.email %></td>
    <td><%= link_to 'Show', catalogue %></td>
    <td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
    <td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    <td><%= link_to "Add to cart", controller: "cart", action: "add", id: @catalogue.id, method: :post %></td>
  </tr>
<% end %>
</table>

<br />
<%= link_to "View cart", controller: "cart", action: "index" %>

<%= link_to 'New Catalogue', new_catalogue_path %>

【问题讨论】:

  • 在 routes.rb 中使用小写的 put 和 delete 方法,而不是 PUT 和 DELETE
  • 嗯,它成功了...现在可以运行服务器了...我原以为放低或放高并删除不会有什么不同
  • 检查我的答案,为什么它应该是小写的

标签: ruby-on-rails ruby-on-rails-3 routes


【解决方案1】:

在您的 routes.rb 文件中,您已经使用大写的 PUT 和 DELETE 定义了路由。以小写形式定义路由,例如 put 和 delete,因为这些是 ActionDispatch::Routing::Mapper 类的方法,它们被调用来为您的应用程序设置路由。由于 ruby​​ 区分大小写,因此您需要以正确的大小写调用该方法。

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2014-08-14
    相关资源
    最近更新 更多