受保护的方法'update'调用#<project

时间:2018-02-13 20:50:44

标签: ruby-on-rails ruby docker jhipster swagger-codegen

=“”

我使用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

1 个答案:

答案 0 :(得分:2)

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

  

update_attributes(attributes)⇒对象

     

使用传入的Hash中的所有属性更新此资源,并请求记录   保存。