“Rails: NoMethodError for Nested Attributes in Form Submission

Description:

I’m working on a Rails application where I have a Picture model that has a nested Pictureclass model. The index (the gallery of pictures) and the show (a detail of the picture) are showing their category, tipu, and style values. The form for editing/creating also works well, but when I try to create/update a Picture record, I encounter the following error:

NoMethodError in PicturesController#update
undefined method `category_id’ for an instance of Picture

Controller

class PicturesController < ApplicationController
  before_action :set_picture, only: %i[ show edit update destroy ]

  def index
    @pictures = Picture.where('start_date >= ?', Date.today).order(start_date: :asc)
    @pictureclasses = Pictureclass.where(picture_id: @pictures.pluck(:id)).includes(:category, :ritme, :tipu)
    @categories = @pictureclasses.map(&:category)
    @styles = @pictureclasses.map(&:style)
    @tipus = @pictureclasses.map(&:tipu)
  end

  def show
    @pictureclasses = Pictureclass.where(picture_id: @picture.id).includes(:category, :ritme, :tipu)
    @categories = @pictureclasses.map(&:category)
    @styles = @pictureclasses.map(&:style)
    @tipus = @pictureclasses.map(&:tipu)
  end

  def edit
    @picture = Picture.find_by(slug: params[:id])
    @picture.build_pictureclass if @picture.pictureclass.nil?
    @pictureclasses = Pictureclass.where(picture_id: @picture.id).includes(:category, :ritme, :style)
  end

  def update
    if @picture.update(picture_params)
      redirect_to @picture, notice: "Picture was successfully updated."
    else
      render :edit, status: :unprocessable_entity
    end
  end

  private

  def set_picture
    @picture = Picture.friendly.find(params[:id])
  end

  def picture_params
    params.require(:picture).permit(:start_date, :start_time, :start_point, :title, :descripcio, :ruta_foto, :Km, :slug, :max_inscrits, :min_inscrits, :num_dies, :fi_ndies, :oberta, pictureclass_attributes: [:id, :category_id, :tipu_id, :style_id])
  end
end

Models:

class Picture < ApplicationRecord
  has_one :pictureclass, dependent: :destroy
  accepts_nested_attributes_for :pictureclass, allow_destroy: true
  has_one :category, through: :pictureclass
  has_one :tipu, through: :pictureclass
  has_one :style, through: :pictureclass
end

class Pictureclass < ApplicationRecord
  belongs_to :category
  belongs_to :style
  belongs_to :tipu
  belongs_to :picture
end

class Category < ApplicationRecord
  has_many :pictureclasses, dependent: :destroy
  has_many :pictures, through: :pictureclasses
end

class Tipu < ApplicationRecord
  has_many :pictureclasses, dependent: :destroy
  has_many :pictures, through: :pictureclasses
end

class Style < ApplicationRecord
  has_many :pictureclasses, dependent: :destroy
  has_many :pictures, through: :pictureclasses
end

Form

<%= form_with(model: @picture, class: "contents text-sm") do |form| %>
  <%= form.fields_for :pictureclass do |pictureclass_form| %>
    <div class="field">
      <%= pictureclass_form.label :category_id %>
      <%= pictureclass_form.collection_select :category_id, Category.all, :id, :name %>
    </div>
    <div class="field">
      <%= pictureclass_form.label :tipu_id %>
      <%= pictureclass_form.collection_select :tipu_id, Tipu.all, :id, :name %>
    </div>
    <div class="field">
      <%= pictureclass_form.label :style_id %>
      <%= pictureclass_form.collection_select :style_id, Style.all, :id, :name %>
    </div>
  <% end %>
<% end %>

Steps Taken:

  • Ensured that accepts_nested_attributes_for is set up in the Picture model.
  • Verified that the form is correctly structured to handle nested attributes.
  • Checked that the picture_params method permits the nested attributes.

Question:

What might be causing the NoMethodError for category_id when updating a Picture record, and how can I resolve it?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật