Rails Cocoon Gem-双嵌套资源



我有一个Article,它有一个嵌套的资源Image Gallery。一个Image Gallery具有一个嵌套的资源Image Gallery Image。我已经为Articles设置了这样的参数:

params.require(:article).permit(
    :featured_image, :title, :description, :department_id, :order_number,
    image_galleries_attributes: [:id, :order_number, :_destroy,
        image_gallery_images_attributes: [:id, :order_number, :caption, :_destroy]
    ]
)

我目前正在使用Cocoon Gem,并尝试在表单输入中使用双嵌套资源,如下所示:

= f.fields_for :image_galleries do |gallery|
  %h4 Image Gallery
  = gallery.input :order_number, placeholder: "1"
  = link_to_remove_association "Remove", gallery, class: "btn-remove"
  = gallery.simple_fields_for :image_gallery_images do |image|
    = render 'image_gallery_image_fields', image: image
  = link_to_add_association 'add', gallery, :image_gallery_images

_image_gallery_image_fields.html.haml

= image.input :order_number
= link_to_remove_association "Remove", image

即使将局部变量传递到渲染视图,我仍然会得到以下错误:

undefined local variable or method `image'

使用嵌套资源时,是否需要以不同的方式传递变量?或者,是否有任何原因需要以不同的方式使用自动使用的模板(_image_gallery_image_fields.html.haml)?

感谢您提供的任何帮助!

更新

在Article Controller中创建方法。

def create
    @article = @issue.articles.create(article_params)
    @article.user_id = current_user.id if current_user
    @article.save
    if @article.save
        flash[:notice] = "Article was successfully created"
        redirect_to edit_admin_issue_article_path(@issue, @article)
    else
        render 'new'
    end
end

我认为您需要在允许的params上放置一个名为image的字段。此字段将具有数据库中图像的路径。你用什么上传图片?

相关内容

  • 没有找到相关文章

最新更新