ruby-on-rails-3之我怎样才能让 sencha 架构师从 Rails 中读取 json

zlslch 阅读:645 2024-02-20 12:42:16 评论:0

我有一个 Ruby on Rails 3 新闻应用程序。它需要一个标题、一些内容和一个图像附件(使用回形针 gem )。我正在使用 RABL gem 为 Sencha Architect 项目提供 JSON feed .

问题是 Sencha Architect 无法读取 JSON url。当我尝试将数据加载到商店时,我收到一条非常无用的消息,说“无法使用提供的配置加载数据”和一个在浏览器中打开 url 的链接(打开得很好)。 Sencha Architect 在某处是否有提供有关此错误的更多信息的日志?

以下是 RABL 文件:

# show.rabl 
object @article 
attributes :title, :body 
node(:mobile_url) { |article| article.image.url(:mobile, false) } 
node(:tablet_url) { |article| article.image.url(:tablet, false) } 
node(:original_url) { |article| article.image.url(:original, false) } 
 
# index.rabl 
collection @articles 
extends "articles/show" 

RABL 默认不提供内容类型,所以我在文章 Controller 中有这个:
before_filter :default_format_json 
 
def default_format_json 
  if(request.headers["HTTP_ACCEPT"].nil? && params[:format].nil?) 
    request.format = "json" 
  end 
end 

这是 Architect 生成的代码,用于显示我使用的配置选项:
# Stories store 
Ext.define('MyApp.store.Stories', { 
    extend: 'Ext.data.Store', 
 
    requires: [ 
        'MyApp.model.Story' 
    ], 
 
    config: { 
        autoLoad: true, 
        model: 'MyApp.model.Story', 
        storeId: 'Stories', 
        proxy: { 
            type: 'jsonp', 
            url: 'https://feedr.org.uk/articles.json', 
            reader: { 
                type: 'json', 
                rootProperty: 'article' 
            } 
        } 
    } 
}); 
 
# Story model 
Ext.define('MyApp.model.Story', { 
    extend: 'Ext.data.Model', 
 
    config: { 
        fields: [ 
            { 
                name: 'title' 
            }, 
            { 
                name: 'body' 
            }, 
            { 
                name: 'mobile_url' 
            }, 
            { 
                name: 'tablet_url' 
            } 
        ] 
    } 
}); 

如何让 Sencha Architect 读取我的 JSON 数据?非常感谢您能提供的任何帮助。

请您参考如下方法:

好吧,事实证明 Sencha 配置没有问题,问题出在我的 rails 应用程序上。我正在使用 RABL gem 生成 Json 响应。 RABL 需要配置为以初始化程序的形式生成 JsonP 响应,如下所示:

# config/initializers/rabl_init.rb 
Rabl.configure do |config| 
  config.include_json_root = false 
  config.include_xml_root  = false 
  config.enable_json_callbacks = true 
end 

重要的选项是 enable_json_callbacks = true .它导致 RABL 将响应作为 JsonP 提供,我的 Sencha 应用程序可以理解。


标签:架构师
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号