lundi 16 mars 2020

trying to use the form object design pattern but getting this error: ActiveModel::UnknownAttributeError in PersonalTrainersController#create

I'm very new to rails so dont really know what I am doing but I'm trying to use the form object design pattern. I've been trying to debug t for a while now and cant figure it out. This is the form that I am trying to use for the database

class PersonalTrainerForm
    include ActiveModel::Model
    include Virtus.model

    attribute :firstName, String
    attribute :secondName, String
    attribute :desription, String
    attribute :amountOfClients, Integer

    attr_reader :record

    def persist
        @personal_trainer = id ? PersonalTrainer.find(id) : PersonalTrainer.new
        if valid?
            @personal_trainer.save!
            true
        else
            false
        end
    end
end  

This is my controller file

class PersonalTrainersController < ApplicationController
  before_action :set_personal_trainer, only: [:show, :edit, :update, :destroy]

  # GET /personal_trainers
  # GET /personal_trainers.json
  def index
    @personal_trainers = PersonalTrainer.all

  end

  # GET /personal_trainers/1
  # GET /personal_trainers/1.json
  def show
    @personal_trainer_decorator = helpers.decorate(personal_trainer)

  end

  # GET /personal_trainers/new
  def new
    @personal_trainer = PersonalTrainer.new
  end

  # GET /personal_trainers/1/edit
  def edit
  end

  # POST /personal_trainers
  # POST /personal_trainers.json
  def create
    @personal_trainer = PersonalTrainerForm.new(personal_trainer_params)

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

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

  # DELETE /personal_trainers/1
  # DELETE /personal_trainers/1.json
  def destroy
    @personal_trainer.destroy
    respond_to do |format|
      format.html { redirect_to personal_trainers_url, notice: 'Personal trainer was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def personal_trainer_params
      params.require(:personal_trainer).permit(:firstName, :secondName, :desription, :amountOfClients)
    end
end

And finlally this is the full trace.

** app/controllers/personal_trainers_controller.rb:30:in create' actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:insend_action' actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:30:inprocess_action' actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:in block in process_action' activesupport (6.0.2.1) lib/active_support/callbacks.rb:135:inrun_callbacks' actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:inprocess_action' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in block in process_action' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:inblock in instrument' activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in instrument' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:ininstrument' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:inprocess_action' activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in process_action' actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:inprocess' actionview (6.0.2.1) lib/action_view/rendering.rb:39:in process' actionpack (6.0.2.1) lib/action_controller/metal.rb:191:indispatch' actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in dispatch' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:indispatch' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in serve' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:inblock in serve' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in each' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:inserve' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in call' rack-pjax (1.1.0) lib/rack/pjax.rb:12:incall' remotipart (1.4.4) lib/remotipart/middleware.rb:32:in call' warden (1.2.8) lib/warden/manager.rb:36:inblock in call' warden (1.2.8) lib/warden/manager.rb:34:in catch' warden (1.2.8) lib/warden/manager.rb:34:incall' rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in call' rack (2.2.2) lib/rack/etag.rb:27:incall' rack (2.2.2) lib/rack/conditional_get.rb:40:in call' rack (2.2.2) lib/rack/head.rb:12:incall' actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in call' rack (2.2.2) lib/rack/session/abstract/id.rb:266:incontext' rack (2.2.2) lib/rack/session/abstract/id.rb:260:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:incall' activerecord (6.0.2.1) lib/active_record/migration.rb:567:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:inblock in call' activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in run_callbacks' actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:incall' actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:incall' actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in call' web-console (4.0.1) lib/web_console/middleware.rb:132:incall_app' web-console (4.0.1) lib/web_console/middleware.rb:28:in block in call' web-console (4.0.1) lib/web_console/middleware.rb:17:incatch' web-console (4.0.1) lib/web_console/middleware.rb:17:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:incall' railties (6.0.2.1) lib/rails/rack/logger.rb:38:in call_app' railties (6.0.2.1) lib/rails/rack/logger.rb:26:inblock in call' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in block in tagged' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:intagged' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in tagged' railties (6.0.2.1) lib/rails/rack/logger.rb:26:incall' sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:incall' actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in call' rack (2.2.2) lib/rack/method_override.rb:24:incall' rack (2.2.2) lib/rack/runtime.rb:22:in call' activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:incall' actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:incall' rack (2.2.2) lib/rack/sendfile.rb:110:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:incall' webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in perform_request' rack-proxy (0.6.5) lib/rack/proxy.rb:57:incall' railties (6.0.2.1) lib/rails/engine.rb:526:in call' puma (4.3.1) lib/puma/configuration.rb:228:incall' puma (4.3.1) lib/puma/server.rb:681:in handle_request' puma (4.3.1) lib/puma/server.rb:472:inprocess_client' puma (4.3.1) lib/puma/server.rb:328:in block in run' puma (4.3.1) lib/puma/thread_pool.rb:134:inblock in spawn_thread' ** If there is anyone that can point me in the right direction that would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire