【问题标题】:respond_with having issue with Rails 5respond_with 遇到 Rails 5 问题
【发布时间】:2017-10-09 07:25:45
【问题描述】:

我正在将我的 Rails 应用程序从 4.1.1 升级到 5.1.4

我正在使用roar-rails gem 来解析和呈现 REST 文档。我面临一些问题,因为responders gem 已被提取到单独的 gem。

respond_with 已移至“响应者”gem。

我的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

我的 Post 代表

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 gem 中,respond_with 方法中有选项,但我认为它无法发送到roar-rails

/path-to-gem/vendor/responders-master/lib/action_controller/respond_with.rb 这是sn-p:

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 ruby-on-rails-5.1 responders rails-roar


    【解决方案1】:

    respond_with 已从 rails 4.2.1 中弃用。

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

    【讨论】:

    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多