0

I saw a similar answered question here which got me this far. But now I am facing an error in Form. The solution I am looking for is basically saving to two tables in Ruby Rails where saving the Property with address in first table also saves 2 images in Pictures' second table.

Migration1:

class CreateProperties < ActiveRecord::Migration[5.0]
  def change
    create_table :properties do |t|
      t.string :address

      t.timestamps
    end
  end
end

Migration2:

class CreatePictures < ActiveRecord::Migration[5.0]
  def change
    create_table :pictures do |t|
      t.string :image1
      t.string :image2

      t.timestamps
    end
  end
end

Property Model:

class Property < ApplicationRecord
    has_many :pictures
    accepts_nested_attributes_for :pictures
end

Picture Model:

class Picture < ApplicationRecord
    belongs_to :property
end

PropertiesController:

class PropertiesController < ApplicationController
before_action :set_property

  def new
    @property = Property.new
  end

  def create
    @property = properties.build(property_params)
    if @property.save
      flash[:success] = "Property was successfully created"
      redirect_to property_path(@property)
    else
      render 'new'
    end
  end

  private
    def property_params
      params.require(:property).permit(:address, picture_attributes: [:image1, :image2])
    end
end

The FORM which I don't know is done as below:

<%= form_for(@property) do |f| %>
<%= f.label :address %>
<%= f.text_field :address %>

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

<%= f.label :image2 %>
<%= f.text_field :image2 %>
<%= f.submit %>
<% end %>

Error Picture:

Error on new.html.erb

2
  • Sometimes, a form object is a good alternative to complicated nested forms that model complicated nested relationships. Just search "ruby on rails form object" for more info... Commented Apr 5, 2017 at 23:08
  • Thanks for the tip Brad, will read up on this definitely! Commented Apr 5, 2017 at 23:13

1 Answer 1

2

You should use the fields_for method to have a form for pictures inside the property form:

# inside the property form_for
<%= f.fields_for @property.pictures.build do |p| %>
  <%= p.file_field :image1 %>
  <%= p.file_field :image2 %> 
<% end %> 
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks Amr! will certainly try this out tomorrow and will report back.!
One last thing, in your property_params it's pictures_attributes not picture_attributes. Hope this helps!
yes, i was playing around with that in singular/plural but will stick to plural now!
ok..i tried this in form: <%= form_for(@property) do |f| %> <%= f.label :address %> <%= f.text_field :address %> <%= f.fields_for @property.pictures.build do |p| %> <%= p.file_field :image1 %> <%= p.file_field :image2 %> <% end %> <%= f.submit %> <% end %> but it is giving me error >> unknown attribute 'property_id' for Picture (around line #5)
Rails expects a foreign key field property_id in your pictures table for the association to work. Create a migration and add this field and it should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.