4

정의 방법 '[] '#정의 방법 '[]'번호 <ActionDispatch :: HTTP를 :: UploadedFile : 0x007fae8c0bfaa8> 난이 UserController # 1 업데이트에서 NoMethodError을 발생 만들 때 I에서, file_field 컬럼 형태를 중첩 한

{ 
"utf8"=>"✓", 
"authenticity_token"=>"HFWawKp4RH7+AFV0yQ1cXpzxHDfubKTKkiDiS6QKnJk=", 
"user"=> { 
    "name"=>"Test", 
    "image_attributes"=> { 
    "picture"=>#<ActionDispatch::Http::UploadedFile:0x3f4e318 
    @original_filename="Beautiful Sky_thumb.jpg", 
    @content_type="image/jpeg", 
    @headers="Content-Disposition: form-data; name=\"user[pics_attributes][pic]\"; filename=\"Beautiful Sky_thumb.jpg\"\r\nContent-Type: image/jpeg\r\n", 
    @tempfile=#<File:C:/Users/Alex/AppData/Local/Temp/RackMultipart20120824-4784-5rmxid>> 
    } 
}, 
"commit"=>"Save User" 
} 

내 모델

class User < ActiveRecord::Base 
    attr_accessbile: :name, image_attributes 
    has_many :images 
    accepts_nested_attributes_for: images 
end 

class Image < ActiveRecord::Base 
    belongs_to :verve_app 
    mount_uploader picture, PictureUploader 
end 

내 컨트롤러에 대한

def new 
    @user = User.new 
    @user.images.build 
end 

def create 
    @user = User.new(params[:user]) 
    @user.save! 
end 

내보기

<%= form_for @user do |f| %> 
    <%= f.hidden_field :name, {value: 'Alex'} %> 
    <%= f.fields_for :image_attributes do |image| %> 
    <%= image.file_field :picture %> 
    <% end %> 
    <%= user.submit %> 
<% end %> 

도와주세요.

답변

0

image_attributes가 아닌 model 클래스 : image에서 fields_for를 호출 해보십시오.

< % = f.fields_for를 바꾸십시오. image_attributes do | image | %>와 < % = f.fields_for : 이미지 do | i | %>. 그런 다음 image.file_field를 i.file_field로 변경하십시오.

Google이 작업하는 데 도움이되는지 아니면 적어도 새로운 오류가 발생하는지 확인하십시오.

0

보기에서 변경하십시오. :image_attributes:images 나머지 코드가 올 바르고 변경하면 올바르게 작동합니다. 아이의 여러 인스턴스 (이미지)를 원한다면 컨트롤러에서 많은 것을 구축하십시오. 당신이 예를 볼 수있는

여기에 관련 티켓입니다 : Plural for fields_for has_many association not showing in view

0

모델 클래스는

mount_uploader :picture, PictureUploader 
:-) 업 로더에 대한 후크를 포함하는 것을 잊지 마세요
관련 문제