我正在尝试从当前的Owin上下文中获取DbContext,因此我可以在我的应用程序上使用单个上下文,但是,我得到一个NullReferenceException.
我可以访问UserManager和RoleManager:
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
Run Code Online (Sandbox Code Playgroud)
他们在Identity示例项目中默认配置了它们的来源:
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
Run Code Online (Sandbox Code Playgroud)
但是试图让上下文直接使用它:
ApplicationDbContext context = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
它总是在我的控制器上返回null.如何从Owin上下文中获取当前的DbContext?
编辑:
我正在创建一个与我的通用存储库一起使用的新上下文
public AdminController()
{
AppContext = new ApplicationDbContext();
this.repoProyects = new GenericRepository<Proyect>(AppContext);
}
Run Code Online (Sandbox Code Playgroud)
但是它创建了一个问题,实体是从多个上下文引用的,所以我试图得到当前的Owin上下文:
public AdminController()
{
this.AppContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
this.repoProyects = new GenericRepository<Proyect>(AppContext);
}
Run Code Online (Sandbox Code Playgroud)
HttpContext从这里始终为null,所以我不知道如何让上下文将它传递给我的类.
我的目标是在ruby中测试我的GraphQL模式的类型,我正在使用graphql-ruby gem。
我找不到任何最佳实践,所以我想知道什么是测试架构的字段和类型的最佳方法。
该gem建议不要直接测试该模式http://graphql-ruby.org/schema/testing.html,但是我仍然觉得知道模式何时意外更改很有价值。
具有这样的类型:
module Types
class DeskType < GraphQL::Schema::Object
field :id, ID, 'Id of this Desk', null: false
field :location, String, 'Location of the Desk', null: false
field :custom_id, String, 'Human-readable unique identifier for this desk', null: false
end
end
Run Code Online (Sandbox Code Playgroud)
我的第一种方法是fields在GraphQL :: Schema :: Object类型中使用哈希,例如:
Types::DeskType.fields['location'].type.to_s => 'String!'
Run Code Online (Sandbox Code Playgroud)
创建一个RSpec匹配器,我可以提出如下测试:
RSpec.describe Types::DeskType do
it 'has the expected schema fields' do
fields = {
'id': 'ID!',
'location': 'String!',
'customId': 'String!'
}
expect(described_class).to match_schema_fields(fields)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这种方法有一些缺点: …
我有一个TimeEntry模型的序列化器,如下所示:
class TimeEntrySerializer < ActiveModel::Serializer
attributes :id, :description, :duration
has_one :project
end
Run Code Online (Sandbox Code Playgroud)
当我只返回所有记录时,它按预期工作:
def index
@time_entries = current_user.time_entries.all
respond_to do |format|
format.html
format.json { render json: @time_entries }
end
end
Run Code Online (Sandbox Code Playgroud)
但是,我想返回按日期组织的条目,如下所示:
[
{ "2016-03-16" => [TimeEntry1, TimeEntry2, TimeEntry3] },
{ "2016-03-17" => [TimeEntry1, TimeEntry2] }
]
Run Code Online (Sandbox Code Playgroud)
我是这样做的:
def self.get_entries_for_user(current_user)
current_user.time_entries
.group_by { |item| item.created_at.to_date.to_s }
.map { |day, entries| { day => entries } }
end
Run Code Online (Sandbox Code Playgroud)
但是现在,序列化程序不适用于TimeEntry对象,我不确定在这种情况下它是否真的应该工作...我想避免自己格式化数据:
def self.get_entries_for_user(current_user)
current_user.time_entries
.group_by { |item| item.created_at.to_date.to_s }
.map do |day, …Run Code Online (Sandbox Code Playgroud) 我正在使用 pg_search gem 将全文搜索添加到我的应用程序中的表中。
我的问题是在尝试创建触发器以在任何更改后使 tsv 列保持最新时,这是我的迁移:
class AddStoreItemsIndex < ActiveRecord::Migration[5.0]
def up
add_column :store_items, :tsv, :tsvector
add_index :store_items, :tsv, using: "gin"
execute <<-SQL
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
ON store_items FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger(
tsv, 'pg_catalog.english', title, full_description, recommended_age, related_intelligences
);
SQL
end
...
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该related_intelligences列是一个数组,因此在尝试查询时出现以下错误:
ActiveRecord::StatementInvalid:
PG::DatatypeMismatch: ERROR: column "related_intelligences" is not of a character type
Run Code Online (Sandbox Code Playgroud)
如何在此 tsv 向量列中包含数组?
我有一个看起来像这样的哈希:
hash = { "data" => {
"Aatrox" => {
"id" => "Aatrox",
"key" => "266",
"name" => "Aatrox"
},
"Ahri" => {
"id" => "Ahri",
"key" => "123",
"name" => "Ahri"
},
"Another name" => {
"id" => "Another name",
"key" => "12",
"name" => "Another name"
},
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从匹配给定键的"id"获取值:
def get_champion_name_from_id(key)
filtered = @champion_data["data"].select do | key, champ_data |
Integer(champ_data["key"]) == key
end
end
Run Code Online (Sandbox Code Playgroud)
我使用select来获取与块匹配的项,但是,返回值是另一个看起来像这样的哈希:
{
"Aatrox": {
"id" => "Aatrox",
"key" => "266",
"name" => "Aatrox" …Run Code Online (Sandbox Code Playgroud) 我正在尝试botkit使用Slack api 创建帖子,但我找不到有关如何Post在Slack中格式化文件的任何文档.
bot.api.files.upload({
content: "# Heading",
filename: "test.md",
filetype: "post",
channels: "random"
});
Run Code Online (Sandbox Code Playgroud)
Markdown格式化不适用于此,是否有任何语法来格式化Slack Posts?
尝试使用HTML,创建的文件的响应如下:
<document><p><document><h1>H1<\/h1><p><\/p><h2>H2<\/h2><p><\/p><h3>H3<\/h3><p><\/p><p>Text <i>italic<\/i> <b>bold<\/b> <a href="<a href=\"http:\/\/www.slack.com%22%3Elink%3C\/a%3E\">http:\/\/www.slack.com">link<\/a><\/a> <u>underline<\/u><\/p><p><\/p><p><strike>strikethrough<\/strike><\/p><p><\/p><p><code>Code Block;<\/code><\/p><\/document><\/p><\/document>
Run Code Online (Sandbox Code Playgroud)
所以它正在逃避html标签.
我试图围绕GraphQL/Relay,我发现很难开始使用Ruby on Rails正确设置一个符合Relay标准的GraphQL API.
我发现了多个如何执行此操作的教程:
https://medium.com/react-weekly/relay-facebook-on-rails-8b4af2057152#.gd8p6tbwi
https://medium.com/@gauravtiwari/graphql-and-relay-on-rails-getting-started-955a49d251de#.m05xjvi82
但他们都提到了graphql-relay目前似乎没有的宝石:https://github.com/rmosolgo/graphql-relay-ruby
该grahql-ruby宝石具有特定于文档中的一个部分继电器,但我发现很难理解设置此通过中继客户消费所需要的.
在Rails中为Relay客户端实现GraphQL API有什么必要?
我正在尝试设置 AWS Codebuild 以将应用程序部署到 AWS Elastic Beanstalk。
问题在于,AWS CodeBuild 似乎无法使用 EB CLI(似乎应该可以,阅读本文... https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli-代码构建.html )
因此,当我在 CodeBuild 上指定 eb cli 命令时:
version: 0.2
phases:
install:
commands:
- echo Logging into Amazon ECR...
- eb init --platform "multi-container-docker-18.03.1-ce-(generic)" --region us-west-1 application
Run Code Online (Sandbox Code Playgroud)
我得到:
/codebuild/output/tmp/script.sh: eb: not found
Run Code Online (Sandbox Code Playgroud)
我还尝试使用以下命令安装 CLI:
- pip install --upgrade awsebcli awscli
- eb init --platform "multi-container-docker-18.03.1-ce-(generic)" --region us-west-1 application
Run Code Online (Sandbox Code Playgroud)
并通过对 Elastic Beanstalk 的完全访问扩展了生成的 AWS CodeBuild 角色
但我收到此错误:
ERROR: NotAuthorizedError - Operation Denied. Access Denied
Run Code Online (Sandbox Code Playgroud)
我--debug在命令中添加了一个标志,这是确切的错误:
2018-09-10 13:33:55,151 …Run Code Online (Sandbox Code Playgroud) 所以我发现了这些问题:
还有这个宝石: https: //github.com/ankane/groupdate
我希望按天对记录进行分组,格式如下:
[
{ "2016-03-16" => [Record1, Record2, Record3] },
{ "2016-03-17" => [Record1, Record2] },
{ "2016-03-18" => [Obj1, Obj2] }
]
Run Code Online (Sandbox Code Playgroud)
我已经能够使用以下代码获得这种格式:
def group_by_criteria
created_at.to_date.to_s
end
list.group_by(&:group_by_criteria).map {|k,v| { k => v} }
Run Code Online (Sandbox Code Playgroud)
但是,正如其他问题中所解释的,这对于大量记录来说不是很有效,我认为我应该使用数据库中的分组,但我不确定如何获取我正在寻找的格式,我尝试过像这样的东西,但我没有得到我所期望的:
list.order("date_trunc('day', created_at) DESC).map{ |k, v| { k => v }}
Run Code Online (Sandbox Code Playgroud)
如何像这样按天对数据库中的记录进行分组?
我有一个在免费套餐中运行在heroku上的机器人,我正在寻找一种方法来在Slack中收到用户的消息时唤醒应用程序.
我的Procfile中有一个Web worker:
web: npm start
Run Code Online (Sandbox Code Playgroud)
我还设置了一个webserver和botkit:
var app = express();
var port = process.env.PORT || 3000;
app.listen(port, function (err) {
if (err) throw err;
console.log('Bot up!');
});
var controller = Botkit.slackbot({
debug: false
});
var bot = controller.spawn({
token: botConfig.SLACK_BOT_KEY
}).startRTM();
Run Code Online (Sandbox Code Playgroud)
机器人正常运行,并在30分钟不活动后空转
2016-09-27T18:55:18.013318+00:00 app[web.1]: info: ** API CALL: https://slack.com/api/rtm.start
2016-09-27T18:55:18.027341+00:00 app[web.1]: Bot up!
2016-09-27T18:55:18.253156+00:00 app[web.1]: notice: ** BOT ID: bot ...attempting to connect to RTM!
2016-09-27T18:55:18.298822+00:00 app[web.1]: notice: RTM websocket opened
2016-09-27T18:55:18.346493+00:00 heroku[web.1]: State changed from starting to up …Run Code Online (Sandbox Code Playgroud) 我正在使用swagger-docs向 API 添加文档,我的配置如下所示:
Swagger::Docs::Config.register_apis({
"1.0" => {
:api_file_path => "public/",
:base_path => "http://localhost:3000",
:clean_directory => true,
:base_api_controller => ActionController::API,
:attributes => {
:info => {
"title" => "Test",
"description" => "Test",
"contact" => "contact@example.com",
"license" => "MIT",
"licenseUrl" => "https://opensource.org/licenses/MIT"
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
这适用于继承自 的控制器ActionController::API,但是当控制器从不同的基本控制器继承时,我如何包含 DSL 方法?例如,当使用knockgem 进行身份验证时:
class Users::AuthenticationController < Knock::AuthTokenController
swagger_controller :login, "Request a new JWT to issue authenticated requests"
swagger_api :create do
summary "Generates a JWT to be used for …Run Code Online (Sandbox Code Playgroud) ruby ×3
postgresql ×2
slack-api ×2
asp.net-mvc ×1
botkit ×1
graphql ×1
graphql-ruby ×1
hash ×1
heroku ×1
pg-search ×1
relay ×1
swagger ×1