Proz1g
Proz1g

Reputation: 1227

Rails - Nested Forms

I'm having a problem with nested forms, I'm new to rails so probably I'm doing something wrong, so I hope you can help me. Thanks in advance.

I have a model Poi (Points of Interest), a model PoiDescription and a model DescriptionType. An instance of Poi has many PoiDescriptions and a DescriptionType has many PoiDescriptions. What I want is this: when I'm creating a new Poi, I want to create multiple descriptions to it. A description has a category associated to it and there can only be one description for each category(ex: 10 categories = 10 descriptions). So here is my code:

Poi model

has_many :poi_descriptions
accepts_nested_attributes_for :poi_descriptions

PoiDescription model

belongs_to :poi
belongs_to :description_type

DescriptionType model

has_many :poi_descriptions

Poi controller

def new
  @poi = Poi.new
  @poi.poi_descriptions.build
  @availableType = DescriptionType.where.not(id: @poi.poi_descriptions.pluck(:description_type_id))
end

def poi_params
  params.require(:poi).permit(:name, :image, :longitude, :latitude, :monument_id, :beacon_id, poi_descriptions_attributes: [:description, :description_type_id, :id])
end

routes

resources :pois do
  resources :poi_descriptions
end
resources :description_types

Poi _form

<%= form_with model: @poi do |f| %>
...
  <%= f.fields_for :poi_descriptions do |p| %>
    <%= p.collection_select :description_type_id, @availableType,:id,:name %>
    <%= p.text_area :description %>
  <% end %>
...

schema

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

create_table "poi_descriptions", force: :cascade do |t|
  t.text "description"
  t.bigint "poi_id"
  t.bigint "description_type_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["description_type_id"], name: "index_poi_descriptions_on_description_type_id"
  t.index ["poi_id"], name: "index_poi_descriptions_on_poi_id"
end

create_table "pois", id: :serial, force: :cascade do |t|
  t.text "name"
  t.float "longitude"
  t.float "latitude"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "monument_id"
  t.integer "beacon_id"
  t.string "image_file_name"
  t.string "image_content_type"
  t.integer "image_file_size"
  t.datetime "image_updated_at"
  t.index ["beacon_id"], name: "index_pois_on_beacon_id"
  t.index ["monument_id"], name: "index_pois_on_monument_id"
end

Now, the problem. Every time I try to create a new Poi I have this error: enter image description here Does anyone know why this is happening? Thanks.

P.S: Sorry for the long post :)

Upvotes: 0

Views: 59

Answers (1)

Proz1g
Proz1g

Reputation: 1227

I've found the solution. Before having the PoiDescription model I had an attribute 'description' on the Poi model and I've put a validation on that attribute. When I changed the model, I forgot to remove that validation and that was the origin of my problem.

Upvotes: 1

Related Questions