0

I am having a trouble that I am new with ROR and want to save some images for the organization using nest attributes or for simplicity just a String in order to try the nested attributes saving in the database but actually it is not saved.

Organization Model

class Organization < ApplicationRecord
has_secure_password
has_many :needs, dependent: :destroy
has_many :org_images, dependent: :destroy
has_many :stringas, dependent: :destroy
accepts_nested_attributes_for :org_images, :reject_if => lambda { |t| t['org_image'].blank? }
accepts_nested_attributes_for :stringas, :reject_if => lambda { |t| t['stringa'].blank? }

Schema

create_table "org_images", force: :cascade do |t|
t.string   "caption"
t.integer  "organization_id"
t.datetime "created_at",         null: false
t.datetime "updated_at",         null: false
t.string   "photo_file_name"
t.string   "photo_content_type"
t.integer  "photo_file_size"
t.datetime "photo_updated_at"
end

  create_table "stringas", force: :cascade do |t|
t.string   "name"
t.datetime "created_at",      null: false
t.datetime "updated_at",      null: false
t.integer  "organization_id"
end

Organization Controller

def new
@organization = Organization.new
3.times {@organization.org_images.build}
@organization.stringas.build # added this
end
  def organization_params
 params.require(:organization).permit(:org_name, :email, :password, :info,:image, :website_URL, :contacts, :logo_url , :password_confirmation  ,stringas_attributes:[:name,:id,:organization_id,:created_at,:updated_at] ,org_images_attributes: [:id,:organization_id,:caption, :photo_file_name, :photo_content_type,:photo_file_size,:photo_updated_at])

end Organization Form

<div class= "field">
<% if builder.object.new_record? %>

  <p>
  <%= builder.label :caption, "Image Caption" %>
  <%= builder.text_field :caption %>
  </p>

  <p>
  <%= builder.label :photo, "Image File" %>
  <%= builder.file_field :photo %>
  </p>

<% end %>
<% if builder.object.new_record? %>

  <p>
  <%= builder.label :name %>
  <%= builder.text_field :name%>
  </p>

<% end %>

<% end %>

Stringa and org_image Models

class OrgImage < ApplicationRecord
belongs_to :organization
has_attached_file :photo, :styles => { :small => "150x150>", :large =>      "320x240>" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
end

class Stringa < ApplicationRecord
belongs_to :organization
end

Organization cotroller create

  def create
@organization = Organization.new(organization_params)

respond_to do |format|
  if @organization.save
    session[:organization_id]=@organization.id
    format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
    format.json { render :show, status: :created, location: @organization }

  else
    format.html { render :new }
    format.json { render json: @organization.errors, status: :unprocessable_entity }
  end
end

end git repository Thanks for your help

  • can you show us the logs or any errors if coming, So that we can have a look why it's not getting saved. – Malware Skiddie Sep 06 '16 at 10:32
  • unfortunately, it saves the organization normally with all its attributes but don't save the nested ones in their tables (In the DB browser I see the record of the organization but the tables of the nested attributes are empty , I will be very thankful if you just showed me a simple example that uses nested model saved normally. – Marwan Alaa Sep 06 '16 at 10:40
  • when I tried to write accepts_nested_attributes_for :stringas only not accepts_nested_attributes_for :stringas, :reject_if => lambda { |t| t['stringa'].blank? } and filled the form of the organization again it gave me the error message "Stringas organization must exist" However I entered the field name of stringas(nested model) in the organization form so I think it is returned blank from the form !! – Marwan Alaa Sep 06 '16 at 11:04

1 Answers1

1

It seems that problem lies in unpermitted org_images attributes in your OrganizationsController. You should add this to your organization_parameters method:

params.require(:organization).permit( ... , org_images_attributes: [:photo, :caption])

EDIT:

After digging a bit deeper I found out that above solution isn't always working. There's an issue on this topic in Rails repo on GitHub. If you want to find nice workaround that'll suit your needs you should read it, or check out this answer.

Community
  • 1
  • 1
ahawrylak
  • 346
  • 3
  • 11