2017-01-03 7 views
0

저는 하루 종일 다양한 솔루션을 시도해 왔습니다. 하지만 아무 것도 작동하지 않는 것 같습니다.레일 폼 정보가 수신되었지만 데이터베이스에 저장되지 않았습니다.

배경 정보 : 내 앱의 경우 사용자가 이미지를 업로드 할 수 있으며 각 이미지에 많은 사용자가있을 수 있습니다. 그래서 다음을 통해 다 대 다 연합입니다. 내가 겪고있는 문제는 양식에서받은 정보/데이터가 데이터베이스에 저장되지 않는다는 것입니다.

나는 고치 보석을 사용하고 있습니다. 이미지를 업로드 할 때 프론트 엔드의 사용자 필드를 추가 및 제거 할 수 있습니다. 이미지를 업로드하고 user1과 user2를 선택하면 디버거가 user_id1과 user_id2가 수신되었음을 보여주었습니다. 제출 한 후 레일 콘솔을 사용하여 image.users를 확인합니다. 그러나 그것은 비어 있습니다. 사용자가 이미지와 관련이 없습니다. 나는 많은 연구를했고 다양한 방법을 시도했지만 아무 것도 효과가없는 것 같습니다.

폼에서 user_ids가 수신되었음을 나타내는 디버거입니다.

{"utf8"=>"✓", "authenticity_token"=>"/q52XogfTcEqCLap3K5C4B4I1gNYQGfBJOynNvdIXUE3ViVnRM+2JcksmaAwCWzBXw2kSOqOR+W25pefgBRRGw==", "image"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x007f49998bd320 @tempfile=#<Tempfile:/tmp/RackMultipart20170103-11352-qmm06k.png>, @original_filename="erincolor4.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image[picture]\"; filename=\"erincolor4.png\"\r\nContent-Type: image/png\r\n">, "image_users_attributes"=>{"1483469430746"=>{"user_id"=>"1", "_destroy"=>"false"}, "1483469434072"=>{"user_id"=>"9", "_destroy"=>"false"}}}, "commit"=>"Upload", "controller"=>"images", "action"=>"create"}

나는 문제가 양식을하지 않습니다 생각하지만, 하나는 모델 또는 컨트롤러입니다.

#Image model 
 
class Image < ActiveRecord::Base 
 
    attr_accessor :picture, :image_users_attributes, :user_id 
 
    has_many :image_users, dependent: :destroy 
 
    has_many :users, through: :image_users 
 

 

 
    accepts_nested_attributes_for :image_users, reject_if: :all_blank, allow_destroy: true 
 

 
    mount_uploader :picture, ImageUploader 
 
    
 
    validates :picture, presence: true 
 
    validates_associated :image_users 
 

 
end 
 
    
 
                   
 
           
 
#User model 
 
class User < ActiveRecord::Base 
 
    
 
    has_many :image_users 
 
    has_many :images, through: :image_users 
 
end 
 

 

 

 
#ImageUser model 
 
class ImageUser < ActiveRecord::Base 
 
    belongs_to :user 
 
    belongs_to :image 
 
    
 
    
 
end
이미지 컨트롤러

class ImagesController < ApplicationController 
 
    
 
    before_action :set_image, only: [:show, :destroy, :edit, :update] 
 
    before_action :require_user, only: [:new, :create, :edit, :update, :destroy] 
 

 

 
    
 
    def new 
 
    @image = Image.new 
 
    
 
    end 
 
    
 
    def create 
 
    debugger 
 
    @image = Image.new(image_params) 
 
    if @image.save 
 
     flash[:success]="Image saved" 
 
     redirect_to user_path(current_user) 
 
    else 
 
     render'new' 
 
    end 
 
    end 
 

 
........... 
 
    
 
private 
 
    
 
    def image_params 
 
    params.require(:image).permit(:picture, image_users_attributes:[:id, :user_id, :_destroy]) 
 
    end 
 
    
 
    
 
    def set_image 
 
    @image =Image.find(params[:id]) 
 
    end 
 
    
 

 
end           

폼보기 :

<%= form_for @image do |f|%> 
 
    <div class="field"> 
 
     <%= f.file_field :picture, accept: 'image/jpeg, image/gif, image/png'%> 
 
    </div> 
 
    <div id='image_users'> 
 
     <%= f.fields_for :image_users do |u| %> 
 
     <%=render 'image_user_fields', :f => u %> 
 
     <% end %> 
 
     <div class="links"> 
 
     <%= link_to_add_association 'add user', f, :image_users %> 
 
     </div> 
 
    </div> 
 
    <%= f.submit 'Upload' %> 
 

 
<% end %> 
 
    
 
#nested form 
 
<div class="nested-fields"> 
 
    <div class="field"> 
 
    <%= f.label :content,"Stylist" %><br> 
 
    <%= f.collection_select :user_id, User.all, :id,:username, :prompt => " " %> 
 
    </div> 
 
    <%= link_to_remove_association "remove stylist", f %> 
 
    
 
</div>

나는 cloud9의 IDE

+0

이미지 저장에 어떤 보석을 사용하고 있습니까? –

+0

gem 'carrierwave' gem 'mini_magick'이미지 사용 – Gweno

답변

0

를 사용하고 그 문제를 알아 냈어. 비디오를 볼 때 attr_accessible을 보았습니다. 그러나 서버를 실행할 때 오류가 발생하여 attr_accessor를 제안했습니다. 그래서 저는 그것을 attr_accessor로 변경했습니다. 이 어리석은 실수를하지 마십시오. 하지만 누군가 내 attr_accessor가 내 코드가 작동하지 않는 이유를 말할 수 있다면 환상적 일 것입니다. :)

+0

실행중인 레일 버전은 무엇입니까? –

+0

It 's rails 4.2.5 – Gweno