无法在Rails中使用验证错误

时间:2013-07-17 09:49:41

标签: ruby-on-rails ruby

我无法获得验证错误。我有一个创建产品页面,其中有两个form_for的One用于产品,一个用于照片。当产品没有上传时,您会收到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

我尝试渲染“new”而不是redirect_to,但我得到NilClass的未定义方法`model_name':Class,错误指向

的照片形式

haml创建产品页面

= 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 }

2 个答案:

答案 0 :(得分:1)

我认为首先你需要了解redirect_to& render

redirect_to :action => 'new' #It will call the method 'new' and then it will call 
                              #respective file. In this case it is `new.haml`


render :action => 'new' #It will call `new.haml` directly without calling method 'new'

因此,当您使用render :action => 'new'时,它将无法获得@photo,因此它会在视图上为NilClass:Class undefined method nil`提供错误either you have to handle model_name'或

当你结束时它会修复 变化

redirect_to new_product_path

@photo = Photo.new
render :action => 'new'

答案 1 :(得分:1)

render使用操作中可用的实例变量 呈现特定视图。例如,如果将渲染用于新操作,则当用户转到/ new时,将调用控制器中的新操作,创建实例变量,然后将其传递给新视图。 Rails为该视图创建html并将其返回给用户的浏览器。这是您认为正常的页面加载。

redirect_to会向用户的浏览器发送重定向,告知其重新申请新网址。然后,浏览器将向该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

更多

Are redirect_to and render exchangeable?

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