Home>

I'm new to Ruby on rails.
Currently, I am building an EC site, and what I stumbled upon was the process of registering in the DB.

I'm thinking of realizing array registration using nested images (plural) and intermediate tables at once.

NameError in ProductsController # create
uninitialized constant Product :: ProductTag
    # Product registration
    def create
        @product_resister = Product.new (product_tag_image_params)  Red line (error location)
        if @ product_resister.save
            flash [: success] = "Product registration is complete."
            redirect_to products_path

The product is not registered in the table.
Looking at the log, I was able to implement it up to the point where it was sent in an array firmly,
I think that the SQL statement may not be issued when saving to the table (or just before that).

Corresponding source code
(views/products/new.html.erb) Partially described
Enter the information of the product I want to register here (multiple tags can be selected below)
<% = Form_for @product_resister, html: {class: "new_product"} do | f |%>
<% = F.label: tag, "tag", class: "form_field"%>
                      <% = collection_check_boxes (: product,: tag_ids, Tag.all,: id,: name) do | tag |%>
                          <% = tag.label do%>
                              <% = tag.check_box%>
                              <% = tag.text%>
                          <% end%>
                      <% end%>
                      <% = f.submit "register", class: "resister_btn"%>
                  <% end%>
(controllers/products_controller.rb)  Partially described
I'm trying to save an array of nested images and selected tags (s) using strong parameters.
    # Product registration page
    def new
        @product_resister = Product.new
        @image = @ product_resister.images.build
    end
    # Product registration
    def create
        @product_resister = Product.new (product_tag_image_params)
        if @ product_resister.save
            flash [: success] = "Product registration is complete."
            redirect_to products_path
        else else
            flash [: danger] = "The product could not be registered. Sorry to trouble you, but please register again."
            render "new"
        end
    end
    private
    def product_tag_image_params
        params.require (: product) .permit (: name,: price,: count,: category,: content,: comment, images_attributes:
        [
            : id,: img1,: img2,: img3,: img4,: img5
        ],,
        tag_ids: []
        )
    end
(models/tag.rb)
class Tag
(models/products_tag.rb)
class ProductsTag
(models/order.rb) * Partially described
class Product
  • Answer # 1

    The error message is as follows.ProductTagThere is no intermediate model called.

    has_many: product_tagsEven though it is, the existing model isProductsTagIt has become. Check the table name and match it to the correct one.