gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:27:07 25 4
gpt4 key购买 nike

我有一个 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 应用程序可以理解。

关于ruby-on-rails-3 - 我怎样才能让 sencha 架构师从 Rails 中读取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13309538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com