[在此處輸入圖片描述][1]
我不斷收到此錯誤訊息,無法弄清楚為什么會這樣。我相信這是因為@resturant 為零。但我不明白為什么它是零。無論如何要“列印或控制臺記錄”回應,以便我可以看到問題是什么[1]:https ://i.stack.imgur.com/sHUrz.png
這是模型
class Resturant < ApplicationRecord
mount_uploader :image, ImageUploader
serialize :image, JSON # If you use SQLite, add this line
belongs_to :user, optional: true
validates :name, :description, :location, :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
這是控制器:
class Resturant < ApplicationRecord
mount_uploader :image, ImageUploader
serialize :image, JSON # If you use SQLite, add this line
belongs_to :user, optional: true
validates :name, :description, :location, :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"}
結尾
# GET /resturants/new
def new
@resturant = Resturant.new
end
# GET /resturants/1/edit
def edit
end
# POST /resturants or /resturants.json
def create
@resturants = Resturant.new(resturant_params)
respond_to do |format|
if @resturant.save
format.html { redirect_to resturant_url(@resturant), notice: "Resturant was successfully created." }
format.json { render :show, status: :created, location: @resturant }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @resturant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /resturants/1 or /resturants/1.json
def update
respond_to do |format|
if @resturant.update(resturant_params)
format.html { redirect_to resturant_url(@resturant), notice: "Resturant was successfully updated." }
format.json { render :show, status: :ok, location: @resturant }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @resturant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /resturants/1 or /resturants/1.json
def destroy
@resturant.destroy
respond_to do |format|
format.html { redirect_to resturants_url, notice: "Resturant was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_resturant
@resturant = Resturant.find(params[:id])
end
# Only allow a list of trusted parameters through.
def resturant_params
params.require(:resturant).permit(:name, :genre, :description, :location, :glutenfree, :vegan, :image, :resturant)
end
結尾
uj5u.com熱心網友回復:
你有一個錯字。(看到這些通常需要第二組新的眼睛。)
您在創建操作中不小心使用了復數形式resturant,但后來又使用了單數形式。
你有:
@resturants = Resturant.new(resturant_params)
將其更改為:
@resturant = Resturant.new(resturant_params)
(您還在整個申請中拼錯了“Restaurant”。您是否愿意解決這個問題由您自己決定。我知道我自己因為某些愚蠢的原因拼錯了那個。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483825.html
標籤:轨道上的红宝石
上一篇:NameError未初始化常量JobsController::Pagination在Ruby中不使用gems
下一篇:Rails:Minitest&CarrierwaveActionView::Template::Error:nil:NilClass的未定義方法“file_name”
