Ruby on Rails Nomethoderror在Fooditemscontroller中#创建
这是我击中我在表单上提交以上传图像时遇到的错误。谁能帮我吗?我觉得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时,图像正在上传。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails DB:迁移
如果找不到标题字段fooditem_table3:运行
rails s
rails db:migrate
if title field not found foodItem_table3: run
rails s