Привет, продолжайте получать эту ошибку в рельсах, когда я пытаюсь создать новый продукт в моем интернет-магазине, ошибка следующая: NameError в "ProductsController # update неинициализированная константа Product :: Categories". Я в основном пытаюсь назначить категорию для продукта, чтобы, например, продукт с именем White polo попадал в категории Tops и т. Д. Он показывает раскрывающийся список со всеми категориями, когда я создаю продукт, но когда я нажимаю на Create, я получите исключение.
Код для моего контроллера следующий:
class ProductsController < ApplicationController
skip_before_filter :authorize, only: [:show]
# GET /products
# GET /products.json
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
# GET /products/1
# GET /products/1.json
def show
@product = Product.find(params[:id])
@cart = current_cart
respond_to do |format|
format.html # show.html.erb
format.json { render json: @product }
end
end
def preview
@product = Product.find(params[:id])
respond_to do |format|
format.html # preview.html.erb
format.json { render json: @product }
end
end
# GET /products/new
# GET /products/new.json
def new
@product = Product.new
@categories = Category.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
@categories = Category.all
end
# POST /products
# POST /products.json
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to products_url,
notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created,
location: @product }
else
format.html { render action: "new" }
format.json { render json: @product.errors,
status: :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.json
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to @product,
notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @product.errors,
status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
def who bought
@product = Product.find(params[:id])
respond_to do |format|
format.atom
end
end
end
Код вида редактирования продукта:
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.collection_select :categories, Category.all, :category_id, :category_name, :include_blank =>'None' %><br />
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, rows: 6 %>
</div>
<div class="field">
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
<div class="field">
<%= f.label :image_url1 %><br />
<%= f.text_field :image_url1 %>
</div>
<div class="field">
<%= f.label :image_url2 %><br />
<%= f.text_field :image_url2 %>
</div>
<div class="field">
<%= f.label :image_url3 %><br />
<%= f.text_field :image_url3 %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :quantity %><br />
<%= f.text_field :quantity %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Что я могу делать не так?