Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails表单在提交失败时不保留嵌套属性_Ruby On Rails_Carrierwave_Nested Forms_Nested Attributes - Fatal编程技术网

Ruby on rails Rails表单在提交失败时不保留嵌套属性

Ruby on rails Rails表单在提交失败时不保留嵌套属性,ruby-on-rails,carrierwave,nested-forms,nested-attributes,Ruby On Rails,Carrierwave,Nested Forms,Nested Attributes,我有一个列出文章的文章表单,包括一个提交新文章的表单,其中包含用于附加照片的嵌套字段 表单工作并保存文章和照片 当验证失败时,我只需呈现正确显示验证错误的索引操作 当我渲染索引操作时,不正确的表单数据将正确显示,但是选定的照片不会保留,因此用户必须更正其数据输入,然后重新附加照片 这是一个问题,因为用户倾向于更正表单数据,然后提交,而没有注意到他们的照片尚未附加 def index @article = Article.new @articles = Article.all end

我有一个列出文章的文章表单,包括一个提交新文章的表单,其中包含用于附加照片的嵌套字段

表单工作并保存文章和照片

当验证失败时,我只需呈现正确显示验证错误的索引操作

当我渲染索引操作时,不正确的表单数据将正确显示,但是选定的照片不会保留,因此用户必须更正其数据输入,然后重新附加照片

这是一个问题,因为用户倾向于更正表单数据,然后提交,而没有注意到他们的照片尚未附加

def index
  @article  = Article.new
  @articles = Article.all
end


def create
  @article = Article.new(discussion_params)
  @article.user_id = current_user.id
  @articles = Article.all

  if @article.save
    redirect_to articles_path
  else
    render "index"
  end
end

private

  # Never trust parameters from the scary internet, only allow the white list through.
  def article_params
    params.require(:article).permit(:content, attachments_attributes: [:id, :file, :_destroy])
  end
附件.rb 提交文章时通过的参数
参数:{“utf8”=>“✓", "真实性令牌“=>”zwVA0Z7q7qj/Dihsatdpihkegy0k2jgtc1arvj5rizqr/OKK/XQRPIXPJIDY1GtyRyHvU3UUUJN47CB+Iw=>”,“文章”=>“{”内容“=>,“附件属性”=>{“0”=>{“文件”=>,“文件缓存”=>“1443854078-3227-6970/YourPhoto 0001.JPG”,“id”=>,“销毁”=>,“假”,提交”}

检查此选项,它可能会帮助您实现完美。这正是我想要的,但它不起作用。文件\u缓存的值为bank。我已更正。file_cache cache的值不是空的,但是附件不会根据需要保留在表单上重新显示。
class Attachment < ActiveRecord::Base
  mount_uploader :file, AttachmentUploader
  belongs_to :attachable, polymorphic: true
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
  params.require(:article).permit(:content, attachments_attributes: [:id, :file, :file_cache, :_destroy])
end
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zwVA0Z7q7qj/diHSatDPihKEGY0K2JGTc1aRwVj5RIZQr/OqK/xQRPIXpjIdY1gzuGtYrgYhVU3uujn47cB+Iw==", "article"=>{"content"=>"", "attachments_attributes"=>{"0"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007f5dce86e8a8 @tempfile=#<Tempfile:/tmp/RackMultipart20151003-3227-1gsru7x.JPG>, @original_filename="YourPhoto_0001.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"article[attachments_attributes][0][file]\"; filename=\"YourPhoto_0001.JPG\"\r\nContent-Type: image/jpeg\r\n">, "file_cache"=>"1443854078-3227-6970/YourPhoto_0001.JPG", "id"=>"", "_destroy"=>"false"}}}, "commit"=>"Submit"}