0

我无法使验证错误起作用。我有一个创建产品页面,其中有两个 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,但我得到了NilClass:Class的未定义方法`model_name',错误指向照片表单

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

2 回答 2

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,因此它会在视图上undefined method为 NilClass:Class either you have to handlenil` 提供错误 model_name' 或

当你改变它会修复

redirect_to new_product_path

@photo = Photo.new
render :action => 'new'
于 2013-07-17T09:52:45.340 回答
1

render使用 action 中可用的实例变量呈现特定视图。例如,如果新操作使用了渲染,当用户转到 /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 回答