i saw similar answered question here got me far. facing error in form. solution looking saving 2 tables in ruby rails saving property address in first table saves 2 images in pictures' second table.
migration1:
class createproperties < activerecord::migration[5.0] def change create_table :properties |t| t.string :address t.timestamps end end end
migration2:
class createpictures < activerecord::migration[5.0] def change create_table :pictures |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 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 don't know done below:
<%= form_for(@property) |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:
you should use fields_for
method have form pictures inside property form:
# inside property form_for <%= f.fields_for @property.pictures.build |p| %> <%= p.file_field :image1 %> <%= p.file_field :image2 %> <% end %>
Comments
Post a Comment