【问题标题】:Rails Cocoon Gem - double nested resourceRails Cocoon Gem - 双嵌套资源
【发布时间】:2016-02-14 14:23:40
【问题描述】:

我有一个Article,它有一个嵌套资源Image GalleryImage 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)?

感谢任何可用的帮助!

更新

在文章控制器中创建方法。

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

【问题讨论】:

  • 您的文章控制器中的创建方法是什么样的?
  • 也许问题是您没有在您的文章控制器中正确地“构建”您的图像?这个答案(适用于 Rails 3,但很清楚)-stackoverflow.com/questions/3784183/…。在您的文章中的新操作类似于:@article.image_galleries.image.build(可能是必需的?)然后您的文章参数将在创建操作中处理其余部分。

标签: ruby-on-rails ruby ruby-on-rails-4 nested-resources cocoon-gem


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多