1

I have an Article, which has a nested resource of Image Gallery. An Image Gallery has a nested resource of Image Gallery Image. I have set up my params for Articles as such:

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]
    ]
)

I am currently using the Cocoon Gem - and have tried to have a double nested resource within the form input, like so:

= 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

Even with having the local variables passed to the rendered view, I still get the following error:

undefined local variable or method `image'

Does a variable need to be passed differently when working with nested resources? Or is there any reason the automatically used template (_image_gallery_image_fields.html.haml) would need to be used differently?

Thanks for any available help!

UPDATE

Create method in 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
Terence Devine
  • 115
  • 1
  • 13
  • What does your create method look like in your Articles controller? – Ben Hawker Nov 13 '15 at 04:04
  • Perhaps the issue is that you are not 'building' your image correctly within your articles controller? This answer (is for Rails 3 but is pretty clear) - http://stackoverflow.com/questions/3784183/rails-3-how-to-create-a-new-nested-resource. In your articles new action something like: @article.image_galleries.image.build (is perhaps required?) Then your articles params will take care of the rest in the create action. – Ben Hawker Nov 13 '15 at 06:14

1 Answers1

0

I think you need to put an field named image on your permitted params. This field will have the path to your image in the database. What are you using to upload the image?

Diego Senott
  • 176
  • 8