0

5 대신이저장 여러 이미지 - 중첩 된 속성 -

this is how the form looks like

이 코드

입니다 추한 외모 때문에 사용이 하나의 클립을 사용하여 여러 이미지를 저장하는 또 다른 방법은 레일이

응용 프로그램/조회/게시물/_form.html.erb

<%= simple_form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %> 
    <%= f.input :title %> 
    <%= f.input :description %> 

     <%= f.simple_fields_for :pictures do |builder| %> 
    <% if builder.object.new_record? %> 

     <%= builder.input :image, :input_html => { :multiple => true } %> 
    <% end %> 
     <% end %> 

    <%= f.input :price %> 
    <%= f.input :city %> 
    <%= f.input :phone %> 

    <%= f.button :submit %> 
<% end %> 

이는이다 posts_controller의 새로운 만들고 행동

응용 프로그램/컨트롤러/posts_controller.rb

def new 
    @post = current_user.posts.build 
    @post.pictures.build 
    end 

    def create 
    @post = current_user.posts.build(post_params) 

    if @post.save 
     params[:image].each do |picture|  

     @post.images.create(:image=> picture) 

     end 
     redirect_to root_path 
    else 
     redirect_to new_post_path 
    end  
    end 
    private 

    def post_params 
     params.require(:post).permit(:title, :price, :description, :phone, :image, :city, pictures_attributes: [:image]) 
    end 

이제 오류 허가되지 않은 매개 변수가 : 이미지

Started POST "/posts" for 127.0.0.1 at 2016-08-18 07:04:27 +0100 
Processing by PostsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"1M2k+ZnouBRdtP6/EDHtLtewiwiS32uSLcZldPNghFt/8n81txtUAAdbKzlCpn6x5l4RJNZYYqV7OVTs6pyvBQ==", "post"=>{"title"=>"setst", "description"=>"setset", "pictures_attributes"=>{"0"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0xa337e44 @tempfile=#<Tempfile:/tmp/RackMultipart20160818-20404-fu0x0v.jpg>, @original_filename="column-01.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[pictures_attributes][0][image][]\"; filename=\"column-01.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0xa337e30 @tempfile=#<Tempfile:/tmp/RackMultipart20160818-20404-1sidf3n.png>, @original_filename="fiy_away.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[pictures_attributes][0][image][]\"; filename=\"fiy_away.png\"\r\nContent-Type: image/png\r\n">]}}, "price"=>"3", "city"=>"setset", "phone"=>""}, "commit"=>"Create Post"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]] 
Unpermitted parameter: image 
    (0.2ms) begin transaction 
    (0.1ms) rollback transaction 
Redirected to http://localhost:3000/posts/new 
Completed 302 Found in 26ms (ActiveRecord: 0.8ms) 

난 단지에 사용하고자하는 파일 필드는 모든 이미지를 추가하고 배열로 저장할 수 있습니다!

내가했던 모든 내가

<%= form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %> 

<%= f.label :title %> 
<%= f.text_field :title %> 

<%= f.label :description %> 
<%= f.text_field :description %> 

     <%= f.fields_for :pictures do |builder| %> 

    <%= f.label :pictures, :class => 'control-label' %> 

    <%= file_field_tag "images[]", type: :file, multiple: true %> 

     <% end %> 
<%= f.label :price %> 
<%= f.text_field :price %> 

    <%= f.label :city %> 
<%= f.text_field :city %> 

    <%= f.label :phone %> 
<%= f.text_field :phone %> 


    <%= f.submit nil, :class => 'btn btn-primary' %> 
<% end %> 

form_for와의 생성 작용에 정상을 simple_form을 제거하고 사용되는이 문제의 해결책을 발견 REPO https://github.com/vogdev/colne2

+0

사용 누에 고치 보석 : https://github.com/nathanvda/cocoon – Emu

+0

이 시도 - http://stackoverflow.com/questions/11605787/uploading-multiple-files-with-paperclip#19379756 –

+0

허가되지 않은 매개 변수 : 이미지를 . 'params.require (: post) .permit (: title, : price, : 설명, : 전화, : image, : 도시, pictures_attributes : [: image]) –

답변

0

입니다 posts_controller

def create 

    @post = current_user.posts.build(post_params) 


    if @post.save 
     if params[:images] 
     #===== The magic is here ;) 
     params[:images].each { |image| 
      @post.pictures.create(image: image) 
     } 
     end 
     redirect_to root_path 
    else 
     redirect_to new_post_path 
    end  
    end 
0

나는 중첩 된 리소스와 레일 5에 대한 ilar 문제, 그리고 난 복수를 추가 발견 : 사실에 file_field에 추가.

두 가지 모델 Medic과 Patient가 있습니다. 환자는, 내가 고안를 사용하고 메딕에서 중첩 된 자원이다, 그래서보기에서 @patient = current_medic.patient

에 액세스 할 수 있습니다

<%= form_for @patient, url: patients_path, html: { multipart: true } do |f| %> 
    <%= f.file_field :image, multiple: true %> 
<% end %> 

편집 - 방법 작성 :

def create 
    @patient = current_medic.patients.new(patient_params) 

    respond_to do |format| 
    if @patient.save 
     format.html { redirect_to @patient, :flash => { :success => 'Patient was successfully created.' } } 
    else 
     format.html { render :new } 
    end 
    end 
end 

희망이 도움!

+0

나에게 create 메소드를 보여줄 수 있습니까 !! –