Ruby on Rails Nomethoderror在Fooditemscontroller中#创建

发布于 2025-02-02 22:21:44 字数 2848 浏览 3 评论 0原文

“

这是我击中我在表单上提交以上传图像时遇到的错误。谁能帮我吗?我觉得JSON序列化不起作用。这导致错误。

class FoodItem < ApplicationRecord
mount_uploader :image, ImageUploader
serialize :image, JSON # If you use SQLite, add this line
belongs_to :user, optional: true

validates :name, :description, :resturant, :glutenfree, :vegan, presence: true 
validates :description, length: {maximum: 1000, too_long: "%{count} characters is the maximum allowed"}

validates :title, length: {maximum: 140, too_long: "%{count} characters is the maximum allowed"}

在这里结束

是我的食物控制器,

  class FoodItemsController < ApplicationController
before_action :set_food_item, only: %i[ show edit update destroy ]
before_action :authenticate_user!, except: [:index, :show]
# GET /food_items or /food_items.json
def index
  @food_items = FoodItem.all.order("created_at desc")
end

# GET /food_items/1 or /food_items/1.json
def show
end

# GET /food_items/new FoodItem.new
def new
  @food_item = current_user.food_items.build
end

# GET /food_items/1/edit
def edit
end

# POST /food_items or /food_items.json FoodItem.new(food_item_params)
def create
  @food_item = current_user.food_items.build(food_item_params)

  respond_to do |format|
    if @food_item.save
      format.html { redirect_to food_items_url(@food_item), notice: "Food item was successfully created." }
      format.json { render :show, status: :created, location: @food_item }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @food_item.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /food_items/1 or /food_items/1.json
def update
  respond_to do |format|
    if @food_item.update(food_item_params)
      format.html { redirect_to food_items_url(@food_item), notice: "Food item was successfully updated." }
      format.json { render :show, status: :ok, location: @food_item }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @food_item.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /food_items/1 or /food_items/1.json
def destroy
  @food_item.destroy

  respond_to do |format|
    format.html { redirect_to food_items_url, notice: "Food item was successfully destroyed." }
    format.json { head :no_content }
  end
end

  # Use callbacks to share common setup or constraints between actions.
  def set_food_item
    @food_item = FoodItem.find(params[:id])
  end

  # Only allow a list of trusted parameters through.
  def food_item_params
    params.require(:food_item).permit(:name, :foodtype, :description, :ingrediants, :resturant, :glutenfree, :vegan, :image)
  end

结束

我相信错误是由于图像未被序列化为JSON引起的,因为当我到达/public/images/tmp时,图像正在上传。

1

This is the error I am getting when I hit submit on my form for uploading an image. Can anyone help me out? I feel like the JSON serialize isnt working. which is causing the error.

class FoodItem < ApplicationRecord
mount_uploader :image, ImageUploader
serialize :image, JSON # If you use SQLite, add this line
belongs_to :user, optional: true

validates :name, :description, :resturant, :glutenfree, :vegan, presence: true 
validates :description, length: {maximum: 1000, too_long: "%{count} characters is the maximum allowed"}

validates :title, length: {maximum: 140, too_long: "%{count} characters is the maximum allowed"}

end

here is my food controller,

  class FoodItemsController < ApplicationController
before_action :set_food_item, only: %i[ show edit update destroy ]
before_action :authenticate_user!, except: [:index, :show]
# GET /food_items or /food_items.json
def index
  @food_items = FoodItem.all.order("created_at desc")
end

# GET /food_items/1 or /food_items/1.json
def show
end

# GET /food_items/new FoodItem.new
def new
  @food_item = current_user.food_items.build
end

# GET /food_items/1/edit
def edit
end

# POST /food_items or /food_items.json FoodItem.new(food_item_params)
def create
  @food_item = current_user.food_items.build(food_item_params)

  respond_to do |format|
    if @food_item.save
      format.html { redirect_to food_items_url(@food_item), notice: "Food item was successfully created." }
      format.json { render :show, status: :created, location: @food_item }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @food_item.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /food_items/1 or /food_items/1.json
def update
  respond_to do |format|
    if @food_item.update(food_item_params)
      format.html { redirect_to food_items_url(@food_item), notice: "Food item was successfully updated." }
      format.json { render :show, status: :ok, location: @food_item }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @food_item.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /food_items/1 or /food_items/1.json
def destroy
  @food_item.destroy

  respond_to do |format|
    format.html { redirect_to food_items_url, notice: "Food item was successfully destroyed." }
    format.json { head :no_content }
  end
end

  # Use callbacks to share common setup or constraints between actions.
  def set_food_item
    @food_item = FoodItem.find(params[:id])
  end

  # Only allow a list of trusted parameters through.
  def food_item_params
    params.require(:food_item).permit(:name, :foodtype, :description, :ingrediants, :resturant, :glutenfree, :vegan, :image)
  end

end

I believe the error is being caused by the image not being serialized to json because when I got to /public/images/tmp the images are being uploaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2025-02-09 22:21:44
  1. 运行Rails DB:迁移如果找不到标题字段fooditem_table
  2. 添加标题添加到food_item_params
 def food_item_params
    params.require(:food_item).permit(:name, :foodtype, :description, :ingrediants, :resturant, :glutenfree, :vegan, :image, :title)
  end

3:运行rails s

  1. run rails db:migrate if title field not found foodItem_table
  2. add title into food_item_params
 def food_item_params
    params.require(:food_item).permit(:name, :foodtype, :description, :ingrediants, :resturant, :glutenfree, :vegan, :image, :title)
  end

3: run rails s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文