Search code examples
ruby-on-railsrubydockerjhipsterswagger-codegen

protected method 'update' called for #<Project


I set up simple Rails application with JHipster microservices. The application has Project model class and CRUD operations on it. JHipster microservices with REST API support developed and integrated with Rails application. Create, Destroy, Show operations are working fine. But update operation throws following error. All the code uploaded to GitHub repository. I looked at similar issues but could not find a proper solution.

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

Solution

  • Project is an ActiveResource not ActiveRecord so that it doesn't have update method. Using update_attributes instead. Ref: 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.