gpt4 book ai didi

ruby-on-rails - protected 方法 'update'调用了#
转载 作者:行者123 更新时间:2023-12-02 21:18:25 26 4
gpt4 key购买 nike

我使用JHipster微服务设置了简单的Rails应用程序。该应用程序具有Project模型类和CRUD操作。具有REST API支持的JHipster微服务已开发并与Rails应用程序集成。创建,销毁,显示操作正常运行。但是更新操作会引发以下错误。所有代码都上传到GitHub repository。我查看了similar issues,但找不到合适的解决方案。

protected method 'update' called for #Project:0x00007fdc9479d3a8

Error Message

projects_controller.rb
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]

# GET /projects
# GET /projects.json
def index
@projects = Project.all
end

# GET /projects/1
# GET /projects/1.json
def show
end

# GET /projects/new
def new
@project = Project.new
end

# GET /projects/1/edit
def edit
end

# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)

respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end



# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update_attributes(project_params)
load(project_params, false) && save
end


# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :location)
end
end

最佳答案

ProjectActiveResource而不是ActiveRecord,因此它没有update方法。改用update_attributes。引用:http://www.rubydoc.info/gems/activeresource/ActiveResource/Base#update_attributes-instance_method

update_attributes(attributes) ⇒ Object

Updates this resource with all the attributes from the passed-in Hash and requests that the record besaved.

关于ruby-on-rails - protected 方法 'update'调用了#<Project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48775408/

26 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com