2015-07-28 101 views
1

我用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 
+2

'SELECT“categories”。* FROM“categories”WHERE“categories”。“ancestry”IS NULL“不是错误,它只是一个SQL查询。什么是真正的错误? –

+0

@WesFoster是的,你是对的。索兹。我从控制台添加了完整的错误。我不知道为什么它不能保存。 – joeyk16

+0

在这种情况下,您可以暂时(!)使用'@ category.save!'而不是'@ category.save'。它引发了描述问题的错误(如果模型验证中有一个错误)。 –

回答

1

您已经添加categories

def item_params 
    params.require(:item).permit(:title, :categories, :price, :description, :image) 
end 

虽然你不应该添加categorypermit清单,因为这是你的输出说。所以正确的代码是:

def item_params 
    params.require(:item).permit(:title, :category, :price, :description, :image) 
end 
+0

你好,我以前试过。我结束了这个错误。 ItemsController中的ActiveRecord :: AssociationTypeMismatch#create 我的模型对我来说很好看我拥有belongs_to:category和has_many:items。 任何线索?此链接表示它与它们有关http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html – joeyk16

+0

尝试将':category'更改为':category_id'。 –

相关问题