сообщение об ошибке: неопределенный метод `create 'для # Вы имели в виду? создано на. Попытка использовать шаблон дизайна политики - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь использовать шаблон разработки политики, но не могу заставить его работать. Это говорит о том, что мой метод создания не определен, но не могу понять, почему. Пожалуйста, помогите: это мой файл политики:

class GymClassPolicy < ApplicationPolicy
    attr_reader :user, :gym_class  
    def initialize(user, gym_class)
      @user = user
      @gym_class = gym_class
    end

    def create?
      user.admin?
    end


  end

Это мой контроллер. Я пытаюсь использовать шаблон разработки политики в методе создания

require 'observer'
require './app/policies/gym_class_policy'


class GymClassesController < ApplicationController
  include Observable
  before_action :set_gym_class, only: [:show, :edit, :update, :destroy]

  # GET /gym_classes
  # GET /gym_classes.json
  def index
    @gym_classes = GymClass.all

  end




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

  # GET /gym_classes/new
  def new
    @gym_class = GymClass.new

  end


  # GET /gym_classes/1/edit
  def edit
  end
  def gym_class_policy
    @_gym_class_policy ||= GymClassPolicy.new(gym_class)
  end 

  # POST /gym_classes
  # POST /gym_classes.json
  def create
    @gym_class = GymClass.new(gym_class_params)
    authorize @gym_class
    if @gym_class.create(gym_class_params)
      redirect_to @post
    else
      render :edit
    end



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




  end


  # PATCH/PUT /gym_classes/1
  # PATCH/PUT /gym_classes/1.json
  def update
    respond_to do |format|
      if @gym_class.update(gym_class_params)
        format.html { redirect_to @gym_class, notice: 'Gym class was successfully updated.' }
        format.json { render :show, status: :ok, location: @gym_class }

      else
        format.html { render :edit }
        format.json { render json: @gym_class.errors, status: :unprocessable_entity }
      end
    end
  end


  # DELETE /gym_classes/1
  # DELETE /gym_classes/1.json
  def destroy
    @gym_class.destroy
    respond_to do |format|
      format.html { redirect_to gym_classes_url, notice: 'Gym class was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def gym_class_params
      params.require(:gym_class).permit(:Type, :PTName, :Date, :Time)
    end
    def Time=(new_Time)
      @Time = new_Time
      notify_observers(self)
    end

end

Наконец, это мой полный след: ** activemodel (6.0.2.1) lib / active_model / attribute_methods.rb: 431: в method_missing' app/controllers/gym_classes_controller.rb:43:in create 'actionpack (6.0.2.1) lib / action_controller / metal / basic_implicit_render.rb: 6: in send_action' actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in process_action' actionpack (6.0.2.1) lib / action_controller / metal / render.rb: 30: в process_action' actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:in блок в process_action 'activesupport (6.0.2.1) lib / active_support / callbacks.rb: 135: в run_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: в блоке process_action' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in в process_action 'activesupport (6.0.2.1) lib / active_support / notifications.rb: 180: в block in instrument' activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in tool' activesupport (6.0.2.1) lib / active_support / notifications.rb: 180: в instrument' 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: в process_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: в process' actionview (6.0.2.1) lib/action_view/rendering.rb:39:in process' actionpack (6.0.2.1) lib / action_controller /metal.rb:191:in dispatch' actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in рассылка ' actionpack (6.0.2.1) lib / action_dispatch / routing / route_set.rb: 51: в dispatch' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in serve 'actionpack (6.0.2.1) lib / action_dispatch / trip / router.rb: 49: в block in serve' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in каждом' actionpack ( 6.0.2.1) lib / action_dispatch / trip / router.rb: 32: в serve' 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: в call' remotipart (1.4.4) lib/remotipart/middleware.rb:32:in call' смотритель (1.2. 8) lib / warden / manager.rb: 36: в block in call' warden (1.2.8) lib/warden/manager.rb:34:in catch 'warden (1.2.8) lib / warden / manager.rb: 34: в call' rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in call' rack (2.2.2) lib / rack / etag.rb: 27: in call' rack (2.2.2) lib/rack/conditional_get.rb:40:in call 'rack (2.2.2) lib / rack / head.rb: 12: in call' 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: in context' 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: in call' 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: в block in call' activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in пакет действий run_callbacks (6.0.2.1) lib / action_dispatch / middleware / callbacks.rb: 26: в call' actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in вызов 'пакет действий (6.0.2.1) lib / action_dispatch / middleware / actionable_exceptions.rb: 17: в call' actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in вызовите веб-консоль (4.0.1) lib / web_console / middleware.rb: 132: в call_app' web-console (4.0.1) lib/web_console/middleware.rb:28:in заблокируйте вызов веб-консоль (4.0.1) lib / web_console / middleware.rb: 17: в catch' 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: в call' 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: в блоке block in call' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in в теговом 'activesupport (6.0.2.1) lib / active_support / tagged_logging.rb: 28: в tagged' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in теговом' railties (6.0.2.1) lib /rails/rack/logger.rb:26:in call' 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: в call' 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:in call' rack (2.2.2) lib/rack/runtime.rb:22:in call 'activesupport (6.0.2.1) lib / active_support / cache / policy / local_cache_middleware.rb: 29: в call' 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:in call' rack (2.2.2) lib/rack/sendfile.rb:110:in call 'actionpack (6.0.2.1) lib / action_dispatch / middleware / host_authorization.rb: 83: в call' webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in execute_request' стоечный прокси-сервер (0.6.5) lib / rack / proxy.rb: 57: в call' railties (6.0.2.1) lib/rails/engine.rb:526:in вызов 'puma (4.3.1) lib / puma / configuration.rb: 228: в call' puma (4.3.1) lib/puma/server.rb:681:in handle_request' puma (4.3.1) lib / puma / server.rb: 472: в process_client' puma (4.3.1) lib/puma/server.rb:328:in блоке в run 'puma (4.3.1) lib / pu ma / thread_pool.rb: 134: в блоке spawn_thread **

1 Ответ

0 голосов

Измените create на save в app / controllers / gym_classes_controller.rb: 43

@gym_class = GymClass.new(gym_class_params)
authorize @gym_class
if @gym_class.save(gym_class_params)
  redirect_to @post
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...