rails form info received but didn't save in the database

56 views Asked by At

I've been trying for different solutions for the entire day. But nothing seems to work

Background info: For my app, users can upload images and each image can have many users. So it's a many-to-many-association through: . The problem I'm encountering is that the info/data received from the form doesn't save in the database.

I'm using the cocoon gem. When upload the image, I'm able to add and remove the users field in the frontend. When I upload the image, and select user1 and user2, debugger showed me that user_id1 and user_id2 are received. After submitting, I use rails console to check image.users. But it's empty, there're no users associate with the image. I did a lot of researching,and tried many different ways but nothing seems to work.

Debugger showing that the user_ids are received from the form.

{"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"} 

I suspect the problem is not the form, but either it's the model or controller.

#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

Image Controller

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 view:

<%= 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>

I'm using cloud9 IDE

1

There are 1 answers

1
Gweno On

I figured out the problem. As I watch videos, I saw attr_accessible, however when i run the server, it threw me an error and suggested attr_accessor. So I changed it to attr_accessor. Don't make this silly mistake. But if anyone can tell me why that attr_accessor made my code not work, that would be fantastic :)