受保护的方法 'update' 调用了 #<Project
protected method 'update' called for #<Project
我使用 JHipster 微服务设置了简单的 Rails 应用程序。该应用程序具有 Project
模型 class 和 CRUD 操作。具有 REST API 支持的 JHipster 微服务已开发并与 Rails 应用程序集成。创建、销毁、显示操作运行良好。但是更新操作会抛出以下错误。所有代码都上传到 GitHub repository. I looked at similar issues 但找不到合适的解决方案。
protected method 'update' called for #Project:0x00007fdc9479d3a8
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
Project
是 ActiveResource
而不是 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 be
saved.
我使用 JHipster 微服务设置了简单的 Rails 应用程序。该应用程序具有 Project
模型 class 和 CRUD 操作。具有 REST API 支持的 JHipster 微服务已开发并与 Rails 应用程序集成。创建、销毁、显示操作运行良好。但是更新操作会抛出以下错误。所有代码都上传到 GitHub repository. I looked at similar issues 但找不到合适的解决方案。
protected method 'update' called for #Project:0x00007fdc9479d3a8
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
Project
是 ActiveResource
而不是 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 be saved.