'위치'모델에 여러 이미지를 업로드하려고합니다. 나는 이미지 모델을 '자산'이라고 불렀다. 한 위치에 여러 애셋이 있습니다. 또한 클립을 사용하여 업로드를 처리하고 nested_form을 사용하여 여러 애셋을 선택할 수 있습니다.레일 4 : 클립으로 여러 이미지 업로드
이상하게도 위치 해시는 변수를 올바르게 전달하는 것으로 보이지만 애셋 모델에서 선택되지 않은 것처럼 보입니다. 어떤 도움이라도 좋을 것입니다!
위치 모델
class Location < ActiveRecord::Base
has_many :location_post
has_many :posts, :through => :location_post
has_many :assets, dependent: :destroy
attr_accessor :asset, :assets_attributes
accepts_nested_attributes_for :assets, :allow_destroy => true
end
자산 모델
class Asset < ActiveRecord::Base
belongs_to :location
has_attached_file :asset,
:styles => {
:blurred => "600x300^",:large => "600x600>", :medium => "250x250^" , :thumb => "100x100^"},
#:source_file_options => {:all => '-rotate "-90>"'},
:convert_options => {
:all => '-auto-orient', :blurred => "-blur 0x6 +repage -resize 600x300^"
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => "[bucketname]",
:path => "/:style/:id/:filename"
validates_attachment_content_type :asset, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
위치 컨트롤러
class LocationsController < ApplicationController
...
def new
@location = Location.new
@location.assets.build
@georesult = Geocoder.search(params[:query])
end
def create
@location = Location.find_or_create_by(name: location_params[:name])
respond_to do |format|
if @location.save
format.html { redirect_to @location, notice: ' <borat voice> Great success! </borat voice>' }
format.json { render :show, status: :created, location: @location }
else
format.html { render :new }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /locations/1
# PATCH/PUT /locations/1.json
def update
respond_to do |format|
if @location.update(location_params)
format.html { redirect_to @location, notice: 'Location was successfully updated.' }
format.json { render :show, status: :ok, location: @location }
else
format.html { render :edit }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
end
...
private
# Use callbacks to share common setup or constraints between actions.
def location_params
params[:location].permit(:name, :notes, :longitude, :country, :latitude, :query, assets_attributes: [ :asset, :asset_content_type, :asset_file_name, :tempfile, :asset_file_size, :asset_updated_at, :_destroy])
end
end
폼보기
<%= nested_form_for(@location, :html=> {:multipart => true}) do |f| %>
...
<%= f.fields_for :assets do |a| %>
<%= a.file_field :asset %>
<%= a.link_to_remove "Remove this image" %>
<% end %>
<%= f.link_to_add "Add an image", :assets %>
...
<%= f.submit "Submit", :class => "btn btn-success submit_location" %>
<% end %>
로그 출력 내가 코드에서 문제의 커플을 볼
Processing by LocationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"n4spoLjq4B3sZSJjqsGFRVjkseOwGgvquAHATBRG1Nk=", "location"=>{"name"=>"York", "notes"=>"", "lat
itude"=>"53.96230079999999", "longitude"=>"-1.0818844", "country"=>"", "assets_attributes"=>{"0"=>{"asset"=>#<ActionDispatch::Http::UploadedFile
:0x007ff739b7bb68 @tempfile=#<Tempfile:/var/folders/sc/gps8hkgj7yg31j81gpnfg9h00000gn/T/RackMultipart20140706-43312-kdpghs>, @original_filename=
"78509.max1024.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"location[assets_attributes][0][asset]\"; filen
ame=\"78509.max1024.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}}, "commit"=>"Submit", "id"=>"240"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Location Load (0.4ms) SELECT "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT 1 [["id", 240]]
(0.2ms) BEGIN
(0.3ms) COMMIT
Redirected to http://localhost:3000/locations/240
Completed 302 Found in 9ms (ActiveRecord: 1.6ms)
이 params.require'에'location_params'을 변경해보십시오 도움이되기를 바랍니다 [: 자산 , : asset_content_type, : asset_file_name, : 임시 파일, : asset_file_size, : asset_updated_at, : _destroy]) ' – Pavan
작업이 완료 되었습니까? params를 전달하거나 생성 된 객체에 save를 호출하지 않는다고 생각합니다. – mus
@Pavan 고마워요.하지만 주사위는 없어요. 로그에는 해시가 올바르게 만들어 졌음이 표시되지만 제대로 응답하지 않습니다. 다른 아이디어? –