Carrierwave only upload image when update action when using Rails 4 and nested form -
my model:
class product < activerecord::base has_many :product_images, dependent: :destroy accepts_nested_attributes_for :product_images, :reject_if => lambda { |p| p['image'].blank? }, :allow_destroy => true end class productimage < activerecord::base belongs_to :product mount_uploader :image, productimageuploader validates_presence_of :image end
my controller:
def create @product = product.new(permitted_params.product) if @product.save redirect_to edit_admin_product_path(@product), notice: "success" else render :new end end def update @product = product.find(params[:id]) if @product.update_attributes(permitted_params.product) redirect_to edit_admin_product_path(@product), notice: "success" else render :edit end end
permitted_params:
class permittedparams < struct.new(:params) def product params.require(:product).permit(*product_attrs) end def product_attrs [:name, :content, :stock, :list_price, :selling_price, :bonus, :is_added, product_images_attributes: [:id, :image, :_destroy] ] end end
and parameters passed:
parameters: {"utf8"=>"✓", "authenticity_token"=>"eqweq1231sda+3t0131643guxybt75x6nqn1ng=", "product"=>{"name"=>"weqwe", "content"=>"qweq", "product_images_attributes"=>{"0"=>{"image"=>"1069218_513152615405563_1187314087_n.jpg", "_destroy"=>""}}, "stock"=>"", "list_price"=>"", "selling_price"=>"123", "bonus"=>""}, "commit"=>"submit"}
obviously image pass params. when create product rollback alarm image empty(validate presence image in productimage model).
if delete validation create product. can upload images in update action. have totally no idea what's problem! please help. q_q
Comments
Post a Comment