проблемы с сохранением данных из вложенных атрибутов в rails

#ruby-on-rails #ruby #nested-attributes

#ruby-on-rails #ruby #вложенные атрибуты

Вопрос:

У меня абсолютный кошмар, пытающийся сохранить эти модели. Родительская модель называется Galleries, а дочерняя — exhibition images. Форма является вложенной. Когда я пытаюсь обновить атрибуты в форме, они не сохраняются. Я не уверен точно, в чем проблема. Помощь была бы очень признательна.

 class GalleriesController < ApplicationController

  def new
    @gallery = Gallery.new
  end

  def create
  end

  def update
    @gallery = Gallery.friendly.find params[:id]
    if @gallery.save(gallery_params)
      redirect_to '/'
    else
      render '/new'
    end
  end

  def edit
    @gallery = Gallery.friendly.find params[:id]
    @image = 3.times { @gallery.exhibition_images.new }
  end

  def destroy
  end

  def index
    @gallery = Gallery.all
  end

  def show
  end

  private 

    def gallery_params
      params.require(:gallery).permit(:title, :id, exhibition_images_attributes: [:image, :id])
    end

end
  

Модель галереи

 class Gallery < ActiveRecord::Base
  extend FriendlyId
    friendly_id :title, use: :slugged
    belongs_to :guide
    has_many :exhibition_images
    accepts_nested_attributes_for :exhibition_images
end
  

Изображения выставки

 class ExhibitionImage < ActiveRecord::Base
    belongs_to :gallery

    has_attached_file :image, styles: { small: "100x100", guide: "500x500" }
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

end
  

редактировать.html.erb

    <h1>Galleries#edit</h1>
<p>Find me in app/views/galleries/edit.html.erb</p>
<%= bootstrap_form_for(@gallery, :html => {:multipart => true}, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
  <%= f.text_field :title %>    

<%= f.fields_for :exhibition_images do |x| %>
    <%= x.file_field :image, help: "Ensure images are minimum 400x400px"  %>

    <% end %>
  <%= f.submit "Create/Update", class: "btn btn-primary" %>
<% end %>
  

журнал после попытки обновления

     Started PATCH "/galleries/david-knight-maurizio-miele-anita-rowell-a-triangle-of-modern-art" for 127.0.0.1 at 2014-07-01 13:25:18  0100
Processing by GalleriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JzFnQgV4ZBoh176xdphPtH/AJsm/BE1N LWtTUTOgx8=", "gallery"=>{"title"=>"FRED FRED", "exhibition_images_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102781b00 @tempfile=#<Tempfile:/var/folders/ns/ry6z7jfd6qg6j8xr2q6dw0yc0000gn/T/RackMultipart20140701-29180-17bovdo>, @original_filename="mag.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="gallery[exhibition_images_attributes][0][image]"; filename="mag.png"rnContent-Type: image/pngrn">}}}, "commit"=>"Create/Update", "id"=>"david-knight-maurizio-miele-anita-rowell-a-triangle-of-modern-art"}
  

Обновить:

schema.rb

 create_table "exhibition_images", force: true do |t|
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "gallery_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "galleries", force: true do |t|
    t.string   "title"
    t.string   "slug"
    t.integer  "guide_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "guides", force: true do |t|
    t.datetime "date_starting"
    t.datetime "date_ending"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.string   "image_extra_file_name"
    t.string   "image_extra_content_type"
    t.integer  "image_extra_file_size"
    t.datetime "image_extra_updated_at"
    t.string   "title"
    t.text     "description"
    t.text     "extra_info"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "slug"
  end

  add_index "guides", ["slug"], name: "index_guides_on_slug", using: :btree

  create_table "images", force: true do |t|
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "gallery_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  

Комментарии:

1. Предоставьте лучшее описание ТОГО, ЧТО не так с вашим выводом, что вы пробовали и что именно не работает

2. @23tux нет выходных данных, когда я пытаюсь обновить атрибуты, они не сохраняются

3. В консоли нет ошибок? Далее вы отлаживаете это, проверяя, не выполняются ли проверки, и / или ставите перед сохранением и смотрите, вызывается ли обратный вызов.

4. Изначально не вижу ничего неправильного. Возможно, проблема не в этом, но у вас есть идентификатор в вашем gallery_params методе.

5. Можете ли вы опубликовать свою схему?