我正试图让这些模型得以保存,这是一场绝对的噩梦。父模型称为画廊,孩子是展览图像。表单是嵌套的。当我尝试更新表单中的属性时,它们不会保存。我不确定问题究竟在哪里。非常感谢帮助。
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
edit.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\"\r\nContent-Type: image/png\r\n">}}}, "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