Alain Goldman
Alain Goldman

Reputation: 2908

Rails - Routing Error 'No route matches [PUT]'

I'm trying to make a small app that allows users to create a sale. There is a user model product model and photo model. A user has many products and products has many photos. But for some reason after I try to create a product page I get this error.

Routing Error

No route matches [PUT] "/products"

routes.rb

  resources :products
  resources :photos

products controller

  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

new product page (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 routes

       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

Shouldn't this work if I already did resources:products !?

Upvotes: 3

Views: 4045

Answers (3)

Matt
Matt

Reputation: 14038

You have a couple of problems here. Firstly your @photo object isn't saving, which is causing your view to render the new action for the successfully saved @product (thus making the form with a put method since the object is persisted?). It may be because you are setting the photo's product_id after the save instead of before:

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

Try adding the id before the save, see if both objects are then valid.

You still have a bit of a logic problem in that you're redirecting to new if either of the objects fails to save. Instead of doing this, check if both of the objects are valid, then save them if so or redirect if not! Then when redirected to new the object hasn't been saved and the form will be created as a proper post form:

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

Lastly, add the error messages for the object(s) to your new page so you can tell what is invalid.

<% 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 %>

Upvotes: 3

Florian Dano Clement
Florian Dano Clement

Reputation: 257

Check your routes(rake routes) maybe your put routes is not correct and you dont create in your controllers the :

def destroy 

end 

Upvotes: -1

andreofthecape
andreofthecape

Reputation: 1196

Your products controller doesn't contain an edit and update method. PUT is for update.

Upvotes: 0

Related Questions