3

ユーザーがセールを作成できる小さなアプリを作ろうとしています。ユーザーモデルの商品モデルと写真モデルがあります。ユーザーには多くの製品があり、製品には多くの写真があります。しかし、何らかの理由で製品ページを作成しようとすると、このエラーが発生します。

ルーティング エラー

No route matches [PUT] "/products"

ルート.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

レーキルート

       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 を実行している場合、これは機能しないはずです!?

4

3 に答える 3

3

ここにはいくつかの問題があります。まず、 @photo オブジェクトが保存されていないnewため、正常に保存された @product のアクションがビューにレンダリングされます (putオブジェクトが であるため、メソッドを使用してフォームを作成しますpersisted?)。写真の product_id を保存前ではなく保存後に設定している可能性があります。

  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 %>
于 2013-06-04T10:16:24.097 に答える
0

商品コントローラーに edit メソッドと update メソッドが含まれていません。PUT は更新用です。

于 2013-06-04T09:35:00.123 に答える