SELECT“categories”。* FROM“categories”WHERE“categories”。“ancestry”IS NULL

时间:2015-07-28 11:02:39

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我使用Ancestry gem制作了一个分类模型。

当我尝试创建新项目时,我收到此错误。我在我的views / items / New template

中创建项目

控制台中显示错误。在浏览器中,它只是说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

1 个答案:

答案 0 :(得分:1)

您已添加categories

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

虽然您应该将category添加到permit列表中,因为这是您输出的内容。所以正确的代码是:

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