我的应用程序 nil:NilClass 的未定义方法“保存”
undefined method `save' for nil:NilClass for my app
[在此处输入图片描述][1]
我不断收到此错误消息,但无法弄清楚这是为什么。我相信这是因为@resturant 为零。但我不明白为什么它是零。
'print or console log' 是否有响应,所以我可以看到问题是什么
[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
结束
你打错了。 (通常需要第二组新鲜的眼睛才能看到这些。)
您在创建操作中不小心将 resturant
复数化,但后来使用了单数形式。
你有:
@resturants = Resturant.new(resturant_params)
改为:
@resturant = Resturant.new(resturant_params)
(在整个申请中,您还拼错了“Restaurant”。是否要修复此问题取决于您。我知道我自己由于一些愚蠢的原因而无法拼写那个。)
[在此处输入图片描述][1]
我不断收到此错误消息,但无法弄清楚这是为什么。我相信这是因为@resturant 为零。但我不明白为什么它是零。 'print or console log' 是否有响应,所以我可以看到问题是什么 [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
结束
你打错了。 (通常需要第二组新鲜的眼睛才能看到这些。)
您在创建操作中不小心将 resturant
复数化,但后来使用了单数形式。
你有:
@resturants = Resturant.new(resturant_params)
改为:
@resturant = Resturant.new(resturant_params)
(在整个申请中,您还拼错了“Restaurant”。是否要修复此问题取决于您。我知道我自己由于一些愚蠢的原因而无法拼写那个。)