response_with проблема с Rails 5

Я обновляю приложение Rails из 4.1.1 в 5.1.4,

я использую roar-rails гем для разбора и рендеринга REST документов. Я сталкиваюсь с некоторыми проблемами, как responders драгоценный камень был извлечен, чтобы отделить драгоценный камень.

respond_with был перемещен в жемчужину "респондентов".

мой rails 4 код выглядит так:

PostsController:

class PostsController < ApplicationController
  respond_to :json

  def index
    posts = current_user.posts
    respond_with posts, :represent_with => PostsRepresenter, current_user: current_user
  end
end

Мои представители для Почты

module PostsRepresenter
  # Rails 4 code
  # include Roar::Representer::JSON::HAL
  # Rails 5 code (after adding responders) --------
  include Roar::JSON
  include Roar::Hypermedia
  # Rails 5 code --------

  collection(
    :post,
    class: Post,
    extend: PostRepresenter,
    embedded: true)

    link :make do |args|
      p "............. #{args.inspect}"
      # In rails 4, args are coming correctly what is passing from Controller
      # But in rails 5, args is coming `nil`
      posts_path if args[:current_user].can_create?(Post)
    end
end

Почтовый представитель

module PostRepresenter
  # Rails 4 code
  # include Roar::Representer::JSON::HAL
  # Rails 5 code (after adding responders) --------
  include Roar::JSON
  include Roar::Hypermedia
  # Rails 5 code --------

  property :title
  property :description
  property :author

  link :self do |args|
    post_path(id) if args[:current_user].can_read?(self)
  end

  link :remove do |args|
    post_path(id) if args[:current_user].can_delete?(self)
  end

  link :edit do |args|
    post_path(id) if args[:current_user].can_update?(self)
  end
end

Я столкнулся с проблемой args которые проходят через Controller, после rails 5, приближается nil,

Я отладил проблему и обнаружил, что в responders жемчужина, варианты приходят в respond_with метод, но я думаю, что он не мог отправить его roar-rails,

/path-to-gem/vendor/responders-master/lib/action_controller/respond_with.rb Вот фрагмент:

def respond_with(*resources, &block)
  if self.class.mimes_for_respond_to.empty?
    raise "In order to use respond_with, first you need to declare the " \
      "formats your controller responds to in the class level."
  end

  mimes = collect_mimes_from_class_level
  collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
  block.call(collector) if block_given?

  if format = collector.negotiate_format(request)
    _process_format(format)
    options = resources.size == 1 ? {} : resources.extract_options!
    options = options.clone
    options[:default_response] = collector.response
    p "====================== options :: #{options.inspect}"
    # Options are correct till here but coming `nil` on representers 
    (options.delete(:responder) || self.class.responder).call(self, resources, options)
  else
    raise ActionController::UnknownFormat
  end
end

Пожалуйста, дайте мне знать, что нужно сделать здесь, чтобы сделать args доступно в representers

1 ответ

respond_with был осужден за рельсы 4.2.1,

https://apidock.com/rails/ActionController/MimeResponds/respond_with

Другие вопросы по тегам