carrierwave - Rails form not retaining nested attributes upon failed submission -
i have articles form lists articles, includes form submit new article nested fields attaching photos.
the form works , saves both article , photos.
when validation fails render index action displays validation errors correctly.
when render index action incorrect form data displayed correctly selected photos not retained user has correct data entry , reattach photos again.
this problem users tend correct form data submit without noticing photos have not been attached.
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 scary internet, allow white list through. def article_params params.require(:article).permit(:content, attachments_attributes: [:id, :file, :_destroy]) end
attachment.rb
class attachment < activerecord::base mount_uploader :file, attachmentuploader belongs_to :attachable, polymorphic: true end
new discussion controller params
# never trust parameters scary internet, allow white list through. def article_params params.require(:article).permit(:content, attachments_attributes: [:id, :file, :file_cache, :_destroy]) end
params passed submitting article
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"}
Comments
Post a Comment