0

検証エラーが機能しません。製品用と写真用の 2 つの form_for を持つ製品ページの作成があります。製品がアップロードされない場合、redirect_to new_product_path に送信されます

製品コントローラ

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

def create
  check_for_image = Photo.where(:product_id => nil, :user_id => current_user)
  if check_for_images == []
    redirect_to products_new_path, :notice => "Add an image then press start before submit"
  else 
    @product = current_user.products.create(params[:product])
    if @product.save
      Photo.where(:product_id => nil, :user_id => current_user).update_all(:product_id => @product.id)
      render "show", notice: "Product created!"
    else
      redirect_to new_product_path #, :flash => { :error => "Test!" }
      # render "new"
    end
  end
end

redirect_to の代わりに "new" をレンダリングしようとしましたが、NilClass:Class の未定義のメソッド `model_name' を取得し、次の写真フォームを指しているエラーが発生しました

製品ページを作成する

= form_for @photo, :html => { :multipart => true, :id => "fd" } do |f|
  %span Add files...
  = f.file_field :image

= form_for @product,:url => products_path, :html => { id: "fd", multipart: true } do |f| 
  - if @product.errors.any?
    .error_messages
      %h2 Form is invalid
      %ul
        - for message in @product.errors.full_messages
          %li
            = message
  %p
    = f.text_field :name, placeholder: "Name"
  %p
    = f.text_field :price, class: "auto", data: { a_sign: "$ " }, placeholder: "Price" 
  %p
    = f.text_field :description, placeholder: "Description"

  %p.button.start
    = f.submit

製品モデル

validates :user_id, presence: true
validates :name, presence: true, length:  { minimum: 5 }
validates :price, presence: true, numericality: { greater_than_or_equal_to: 3.00 }
4

2 に答える 2

1

renderaction で使用可能なインスタンス変数を使用して、特定のビューをレンダリングします。たとえば、レンダーが新しいアクションに使用された場合、ユーザーが /new に移動すると、コントローラーの新しいアクションが呼び出され、インスタンス変数が作成されてから新しいビューに渡されます。Rails はそのビューの html を作成し、それをユーザーのブラウザーに返します。これは、通常のページ読み込みと見なすものです。

redirect_toユーザーのブラウザにリダイレクトを送信して、新しい URL を再リクエストするよう伝えます。次に、ブラウザはその URL に新しいリクエストを送信し、その URL に対するアクションを実行します。リダイレクトされたという事実は無視されます。リダイレクトの原因となったアクションで作成された変数は、リダイレクトされたビューでは使用できません。フォームで [作成] をクリックすると、オブジェクトが作成され、そのオブジェクトの編集ビューにリダイレクトされると、このようになります。

リダイレクトを行うたびに新しいインスタンスがエラーなしで作成されたため、検証エラーはありませんでした。

変更する必要がある 2 行:

render "show", notice: "Product created!"redirect_to new_product_path

これについては不明です: redirect_to products_new_path, :notice => "Add an image then press start before submそれが何をするのか、アプリがどのように動作するのかが明確ではありません。

あなたのコントローラー:

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

def create
  check_for_image = Photo.where(:product_id => nil, :user_id => current_user)
  if check_for_images == []
    redirect_to products_new_path, :notice => "Add an image then press start before submit"
  else 
    @product = current_user.products.create(params[:product])
    if @product.save
      Photo.where(:product_id => nil, :user_id => current_user).update_all(:product_id => @product.id)
      redirect_to @product, notice: "Product created!"
    else
      render action: "new"
    end
  end
end

もっと:

redirect_to と render は交換可能ですか?

http://blog.markusproject.org/?p=3313

于 2013-07-17T10:51:09.283 に答える