我用Ancestry gem製作了一個Categories模型。SELECT「categories」。* FROM「categories」WHERE「categories」。「ancestry」IS NULL
當我嘗試創建一個新項目時收到此錯誤。我在我的意見/項目/新模板中創建項目
錯誤顯示在控制檯中。在瀏覽器中,它只是說Your item didn't save
下面是錯誤:
Started POST "/items" for ::1 at 2015-07-28 20:50:47 +1000
Processing by ItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Uw1cjXsCeq3MMrQOEhILhXc93uEroonycj/6Mw/9HlISAJ8ifdl5onMzzKlNlFy+8U+yMlPDywID2EePjV1P7A==", "item"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc35a769a88 @tempfile=#<Tempfile:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20150728-2819-1d4voau.jpg>, @original_filename="sony-xperia-z3-full-specification.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[image]\"; filename=\"sony-xperia-z3-full-specification.jpg\"\r\nContent-Type: image/jpeg\r\n">, "category"=>"1", "title"=>"Sony Xperia ", "price"=>"599", "description"=>"Test description"}, "commit"=>"Create new item"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 55]]
Unpermitted parameter: category
Command :: file -b --mime '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-cfcfbf.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]'
Command :: convert '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' -auto-orient -resize "600x600" '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-16x089y'
Command :: file -b --mime '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-16x089y'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]'
Command :: convert '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' -auto-orient -resize "250x250" '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-1oriw0p'
Command :: file -b --mime '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-1oriw0p'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]'
Command :: convert '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw.jpg[0]' -auto-orient -resize "100x" -crop "100x100+0+0" +repage '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-rhc5cl'
Command :: file -b --mime '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-1jy6tnw20150728-2819-rhc5cl'
(0.2ms) begin transaction
Command :: file -b --mime '/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/dd6d79085f81e599b4c0c4e9d3c227fb20150728-2819-b1kuuw.jpg'
(0.1ms) rollback transaction
Category Load (0.2ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."name" ASC
Rendered items/new.html.erb within layouts/application (20.9ms)
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."ancestry" IS NULL
Rendered layouts/_header.html.erb (2.2ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 947ms (Views: 287.6ms | ActiveRecord: 1.0ms)
任何人都知道如何將這種錯誤修復?我已經嘗試了幾個小時,但無法弄清楚。
的意見/項目/ new.html.erb
<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_form_for @item, html: { multipart: true } do |f| %>
<%= f.input :image%>
<%= f.collection_select :category, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
<%= f.input :title%>
<%= f.input :price %>
<%= f.input :description %>
<%= f.button :submit, "Create new item", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
Item.Controller
class ItemsController < ApplicationController
before_action :correct_user_edit, only: [:edit, :update, :destroy]
def index
@item = @user.items.paginate(page: params[:page])
end
def new
@item = Item.new
end
def home
@items = Item.paginate(page: params[:page])
end
def edit
@item = Item.find(params[:id])
@user = User.find(params[:id])
end
def show
@item = Item.find(params[:id])
end
def update
@item = Item.find(params[:id])
if @item.update(item_params)
redirect_to @item
flash[:success] = 'Item was successfully updated.'
else
render "edit"
end
end
def create
@item = current_user.items.build(item_params)
if @item.save
redirect_to @item
flash[:success] = "You have created a new item"
else
flash[:danger] = "Your item didn't save"
render "new"
end
end
def destroy
Item.find(params[:id]).destroy
flash[:success] = "Item deleted"
redirect_to users_url
end
private
def item_params
params.require(:item).permit(:title, :categories, :price, :description, :image)
end
#Check to see if user can edit item.
def correct_user_edit
if @item = current_user.items.find_by(id: params[:id])
else
flash[:danger] = "You can't edit that item"
redirect_to root_url if @item.nil?
end
end
end
分類模式
class Category < ActiveRecord::Base
has_ancestry
has_many :items
end
產品型號
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :category
validates :category, presence: true
validates :title, presence: true, length: { maximum: 30 }
validates :price, presence: true
validates :description, presence: true, length: { maximum: 2000 }
validates :user_id, presence: true
has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
分類控制器
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
def index
@categories = Category.all
end
def show
end
def new
@category = Category.new
end
def edit
end
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :parent_id)
end
end
'SELECT「categories」。* FROM「categories」WHERE「categories」。「ancestry」IS NULL「不是錯誤,它只是一個SQL查詢。什麼是真正的錯誤? –
@WesFoster是的,你是對的。索茲。我從控制檯添加了完整的錯誤。我不知道爲什麼它不能保存。 – joeyk16
在這種情況下,您可以暫時(!)使用'@ category.save!'而不是'@ category.save'。它引發了描述問題的錯誤(如果模型驗證中有一個錯誤)。 –