簡體   English   中英

未定義的局部變量或方法 `permitted_task_attributes' for # <api::v1::taskscontroller< div><div id="text_translate"><p> 我有一個遵循 Spree/Solidus 范式的解決方案,它封裝了允許的參數。 我沒有能力改變它,而是效仿。 但是,我遇到了一個問題,我無法重現 #Api::V1::TasksController:0x0000000000b7c0 的未定義局部變量或方法“permitted_task_attributes”。</p><p> 下面是代碼:</p><p> <strong>控制器/api/v1/task_controller.rb</strong></p><pre> module Api module V1 class TasksController &lt; ApiController def index task = Task.all render json: task end def create task = Task.create:(create_action_params) if task render json: task else render json. task.errors end end private def create_action_params params:require(.task).permit(permitted_task_attributes) # The problem is with this variable `permitted_task_attributes` end end end end</pre><p> <strong>控制器/api_controller.rb</strong></p><pre> class ApiController &lt; ApplicationController before_action:set_api_request private def set_api_request request.format = request.format ==:xml? :xml: :json end end</pre><p> li <strong>b/controller_helpers/strong_parameters.rb</strong></p><pre> module ControllerHelpers module StrongParameters def permitted_attributes PermittedAttributes end delegate(*PermittedAttributes::ATTRIBUTES, to: :permitted_attributes, prefix: :permitted) def permitted_task_attributes permitted_attributes.task_attributes end end end</pre><p> <strong>lib/permitted_attributes.rb</strong></p><pre> module PermittedAttributes ATTRIBUTES = [:task_attributes ].freeze mattr_reader(*ATTRIBUTES) @@task_attributes = [:avatar_url, :description, :recorded_on ] end</pre><p> <strong>錯誤</strong></p><pre>undefined local variable or method `permitted_task_attributes' for #&lt;Api::V1::TasksController:0x0000000000b7c0&gt;</pre><p> 我確實了解permitted_task_attributes無法訪問這個名為 allowed_task_attributes 的方法,但我無法修復它。 我試圖在 controller 中包含ControllerHelpers::StrongParameters ,但我仍然有未初始化的常量錯誤。 我怎樣才能解決這個問題? </p></div></api::v1::taskscontroller<>

[英]undefined local variable or method `permitted_task_attributes’ for #<Api::V1::TasksController

我有一個遵循 Spree/Solidus 范式的解決方案,它封裝了允許的參數。 我沒有能力改變它,而是效仿。 但是,我遇到了一個問題,我無法重現 #Api::V1::TasksController:0x0000000000b7c0 的未定義局部變量或方法“permitted_task_attributes”。

下面是代碼:

控制器/api/v1/task_controller.rb

module Api
  module V1
    class TasksController < ApiController
      def index
        task = Task.all
        render json: task
      end

      def create
        task = Task.create!(create_action_params)
        if task
          render json: task
        else
          render json: task.errors
        end
      end

      private

      def create_action_params
        params.require(:task).permit(permitted_task_attributes) # The problem is with this variable `permitted_task_attributes`
      end
    end
  end
end

控制器/api_controller.rb

class ApiController < ApplicationController
  before_action :set_api_request

  private

  def set_api_request
    request.format = request.format == :xml ? :xml : :json
  end
end

li b/controller_helpers/strong_parameters.rb

module ControllerHelpers
  module StrongParameters
    def permitted_attributes
      PermittedAttributes
    end

    delegate(*PermittedAttributes::ATTRIBUTES,
             to: :permitted_attributes,
             prefix: :permitted)

    def permitted_task_attributes
      permitted_attributes.task_attributes
    end
  end
end

lib/permitted_attributes.rb

module PermittedAttributes
  ATTRIBUTES = [
    :task_attributes
  ].freeze

  mattr_reader(*ATTRIBUTES)

  @@task_attributes = [
    :avatar_url, :description, :recorded_on
  ]
end

錯誤

undefined local variable or method `permitted_task_attributes’ for #<Api::V1::TasksController:0x0000000000b7c0>

我確實了解permitted_task_attributes無法訪問這個名為 allowed_task_attributes 的方法,但我無法修復它。 我試圖在 controller 中包含ControllerHelpers::StrongParameters ,但我仍然有未初始化的常量錯誤。 我怎樣才能解決這個問題?

因此,我已將strong_parameters.rb移出/lib並將其移至 controller 關注點。 我也將allowed_attributes.rb移出/lib目錄。 然后我在 tasks_controller.rb 中包含strong_parameters.rb例如include StrongParameters

問題是 controller 無法識別permitted_task_attributes中的allowed_task_attributes方法,並引發錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM