【问题标题】:Rails - Routing Error 'No route matches [PUT]'Rails - 路由错误'没有路由匹配 [PUT]'
【发布时间】:2013-06-04 09:29:26
【问题描述】:

我正在尝试制作一个允许用户创建销售的小应用程序。有用户模型产品模型和照片模型。一个用户有很多产品,产品有很多照片。但由于某种原因,在我尝试创建产品页面后出现此错误。

路由错误

No route matches [PUT] "/products"

routes.rb

  resources :products
  resources :photos

产品控制器

  def new 
    @product = Product.new
    @photo = Photo.new
  end

  def create
    @product = current_user.products.build(params[:product])
    @photo = current_user.photos.new(params[:photo])

    if @product.save && @photo.save
      @photo.product_id = @product.id
      render "show", :notice => "Sale created!"
    else
      render "new", :notice => "Somehting went wrong!"
    end
  end

  def show
    @product = Product.find(params[:id]) 
  end

新产品页面 (HAML)

%h1 
  create item

= form_for @product,:url => products_path, :html => { :multipart => true } do |f|          
  %p
    = f.label :name
    = f.text_field :name

  %p
    = f.label :description
    = f.text_field :description        
    = f.text_field :ship_price

  %p
    = fields_for :photo, :html => {:multipart => true} do |fp|
    = fp.file_field :image  

  %p.button
    = f.submit

rake 路线

       products GET    /products(.:format)               products#index
                POST   /products(.:format)               products#create
    new_product GET    /products/new(.:format)           products#new
   edit_product GET    /products/:id/edit(.:format)      products#edit
        product GET    /products/:id(.:format)           products#show
                PUT    /products/:id(.:format)           products#update
                DELETE /products/:id(.:format)           products#destroy

如果我已经做了 resources:products ,这不应该工作吗!?

【问题讨论】:

  • 尽量不要将 :url 传递给 form_for。
  • 您确定这里没有其他代码吗?如果对象是persisted,Rails 会将表单设为 put 方法
  • ^ 嘿,马特我应该链接什么代码?用户控制器?
  • @AlainGoldman 产品控制器中的任何前置过滤器、视图或控制器中作用于@product 对象的任何代码,或任何可能从帮助器作用于它的任何代码。
  • 是的,产品控制器有 before_filter :require_login before_filter :current_user, only: [:create, :destory] ​​before_filter :correct_user, only: :destory

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


【解决方案1】:

这里有几个问题。首先,您的@photo 对象没有保存,这导致您的视图为成功保存的@product 呈现new 操作(因此使用put 方法制作表单,因为对象是persisted?)。这可能是因为您将照片的 product_id 设置在 after 保存而不是之前:

  if @product.save && @photo.save
    @photo.product_id = @product.id

尝试在保存之前添加id,看看两个对象是否都有效。

如果其中一个对象保存失败,您仍然会重定向到new,这仍然存在一些逻辑问题。不要这样做,而是检查两个对象是否有效,如果是则保存它们,否则重定向!然后当重定向到new 时,对象还没有保存,表单将被创建为正确的post 表单:

def create
  @product = current_user.products.build(params[:product])
  @photo = current_user.photos.new(params[:photo])
  @photo.product_id = @product.id 

  if @product.valid? && @photo.valid?
    @product.save
    @photo.save
    render "show", :notice => "Sale created!"
  else
    render "new", :notice => "Something went wrong!" # the product object hasn't been saved, this is now the correct form type
  end
end

最后,将对象的错误消息添加到您的新页面,这样您就可以知道什么是无效的。

<% if @product.errors.any? %>
  <%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:

  <ul>
    <% @product.errors.full_messages.each do |error_message| %>
      <li><%= error_message %></li>
    <% end %>
  </ul>
<% end %>

【讨论】:

    【解决方案2】:

    您的产品控制器不包含编辑和更新方法。 PUT 用于更新。

    【讨论】:

    • 那不是完整的产品控制器。为了清楚起见,它被缩短了。
    • 这不是问题。问题是创建表单是使用 PUT 方法而不是 POST 生成的。
    【解决方案3】:

    检查您的路线(耙路线)也许您的放置路线不正确,并且您没有在控制器中创建:

    def destroy 
    
    end 
    

    【讨论】:

    • 这与任何事情有什么关系。我试过了,还是不行。
    • 嗯,问题是当你调用你的表单时,rails 认为你想删除它(因为需要路由 PUT)你能显示 rake 路由吗?也许使用 product_path 而不是 products_path
    • 好的,我刚刚将 rake 路由添加到原始帖子中
    • 将 products_path 更改为 product_path
    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2011-06-16
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多