创建产品后你好我无法编辑它。形式有点复杂,因此很难让编辑工作。
尝试编辑时出错。
Completed 500 Internal Server Error in 16ms
ActionView::Template::Error (undefined method `map' for nil:NilClass):
17:
18: <p><%= f.link_to_add "Add a image", :product_images, :data => { :product_image => "#product_images" } %></p>
19:
20: <%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
21:
22: <% @categories.each do |category| %>
23: <div class='sizes_container' id
这是产品表格。
<%= javascript_include_tag "custom" %>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_nested_form_for @product do |f| %>
<%= f.fields_for :product_images do |product_image| %>
<% if product_image.object.new_record? %>
<%= product_image.file_field(:product_image) %>
<%= product_image.link_to_remove "Remove Image", data: { confirm: "Are you sure you want to delete this image?" } %>
<% else %>
<%= product_image.hidden_field :_destroy %>
<% end %>
<% end %>
<p><%= f.link_to_add "Add a image", :product_images, :data => { :product_image => "#product_images" } %></p>
<%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
<% @categories.each do |category| %>
<div class='sizes_container' id ='sizes_container_for_<%= category.id %>'>
<% category.sizes.each do |size| %>
<%= label_tag size.title %>
<%= f.simple_fields_for :product_sizes do |product_size| %>
<%= product_size.input :quantity %>
<%= product_size.hidden_field :size_id %>
<% end %>
<% end %>
</div>
<% end %>
<%= f.input :title, label:"Title"%>
<%= f.input :price, label:"Price"%>
<%= f.input :description,label:"Description" %>
<%= f.input :size_description, label:"Size Details"%>
<%= f.input :shipping_description, label:"Shipping Details"%>
<%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
<%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
有什么想法吗?我环顾网络,找不到答案。所以任何帮助都会很棒。提前谢谢。
产品控制器
class ProductsController < ApplicationController
before_action :set_product, only: [:edit, :show, :update]
before_action :correct_user_edit, only: [:edit, :update, :destroy]
before_action :logged_in_user, only: [:new, :edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
@categories = Category.preload(:sizes).order(:name)
@product.product_images.build
@product.product_sizes.build
end
def home
@products = Product.paginate(page: params[:page])
end
def edit
end
def show
end
def update
if @product.update(product_params)
redirect_to @product
flash[:success] = 'Item was successfully updated.'
else
render "edit"
end
end
def create
@product = Product.new product_params
@product.user_id = current_user.id
if @product.save
redirect_to @product
flash[:success] = "You have created a new product"
else
flash[:danger] = "Your product didn't save"
render "new"
end
end
def destroy
@product.destroy
flash[:success] = "Product deleted"
redirect_to user_products_path
end
def add_outfit_products
@products = current_user.products
end
def add_outfit_similar_products
@products = current_user.products
end
private
def create_product_images
params["product"]["product_images_attributes"].each do |index, image|
ProductImage.create(product_image: image, product_id: @form.product.id)
end
end
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(
:title,
:price,
:description,
:tag_list,
:category_id,
:size_description,
:shipping_description,
product_images_attributes: [:product_image, :_destroy],
product_sizes_attributes: [:size_id, :quantity]
)
end
def correct_user_edit
if @product = current_user.products.find_by(id: params[:id])
else
redirect_to root_url if @product.nil?
end
end
end
答案 0 :(得分:0)
它似乎是您的@categories = nil
,但应该是@categories = []
数组。
检查您的控制器 - 操作编辑,您定义@categories
?