我正面临着一个迄今为止我无法解决的问题.我创建了一个database class到app/db/mysql/database.php包含以下内容:
<?php
namespace App\Database;
use Symfony\Component\Yaml\Yaml;
class Database{
private static $connection = null;
private function __construct( $host, $base, $user, $pass ){
try{
self::$connection = new PDO("mysql:host=$host;dbname=$base", $user, $pass);
}catch(PDOException $e){
die($e->getMessage());
}
}
public static function get(){
if( self::$connection !== null ){
return self::$connection;
}
$yaml = Yaml::parse(file_get_contents(realpath('./app') . '/database.yml'));
self::$connection = new Database( $yaml['host'], $yaml['base'], $yaml['user'], $yaml['pass'] );
}
}
Run Code Online (Sandbox Code Playgroud)
使用作曲家,我正在自动加载这个类:
{
"autoload" : {
"classmap" : [
"app/libraries",
"app/db"
]
}
}
Run Code Online (Sandbox Code Playgroud)
其中生成 …
我正在尝试测试我的响应拦截器,但我很难搞清楚如何模拟$ window对象.这是我的拦截器代码:
'use strict';
angular.module('Domain.handlers')
.config(function($httpProvider) {
$httpProvider.responseInterceptors.push('UnauthorizedInterceptor');
})
.factory('UnauthorizedInterceptor', function($q, $injector, $window, ENV) {
return function(promise) {
var success = function(response) { return response; };
var error = function(response) {
if (response.status === 401) {
$window.location.href = ENV.account + '/oauth/authorize?client_id=' + ENV.clientId + '&redirect_uri=' + ENV.app + '/oauth/callback&response_type=token';
}
return $q.reject(response);
};
return promise.then(success, error);
};
});
Run Code Online (Sandbox Code Playgroud)
这是我的规格:
'use strict';
describe('Domain.handlers.response', function() {
var UnauthorizedInterceptor,
httpProvider,
$httpBackend,
$http,
token = '123456789';
beforeEach(module('Domain.handlers', function($httpProvider) {
httpProvider = $httpProvider;
}));
beforeEach(inject(function(_UnauthorizedInterceptor_, …Run Code Online (Sandbox Code Playgroud) 我实际上正在玩Laravel 4.现在我在表单帖子上实现了CSRF令牌安全性.
问题是,这实际上并不是在会话中生成的令牌Session::token()总是相同的意义上,所以当我尝试重新提交表单甚至从另一台服务器发布表单时,安全检查不起作用Session::token() != Input::get('_token')( filters.php)
有人已经遇到过这个问题?
编辑:
好的,我找到了解释.对于每个机器/会话,令牌实际上是不同的.它现在更有意义:)感谢大家的帮助
我目前正在开发一个系统,只有当用户不是学生时才需要电子邮件,如果用户是学生则需要用户名.所以这就是我在模型中所做的:
class User < ActiveRecord::Base
validates :email, presence: true, unless: :student?
validates :username, presence: true, if: :student?
end
Run Code Online (Sandbox Code Playgroud)
这在用户名属性上工作正常,但对于电子邮件,我仍然会收到Email cannot be blank错误.我想Devise有自己的电子邮件验证规则.
我怎样才能使这个工作,我的意思是重写Devise验证电子邮件的存在规则?
谢谢
我实际上需要一些来自高级Ruby/Rails开发人员的建议,了解保存has_and_belongs_to_many关联的常用和正确方法是什么?
这是我做的,但对我来说它感觉非常脏,我真的不喜欢在我的应用程序中看到该代码:)
车型/ company.rb
class Company < ActiveRecord::Base
has_and_belongs_to_many :type_taxes
## Add Type taxes association
def insert_types_taxes( types_taxes )
self.type_taxes.clear
self.type_taxes << types_taxes
end
end
Run Code Online (Sandbox Code Playgroud)
车型/ taxe.rb
class Taxes < ActiveRecord::Base
has_and_belongs_to_many :societes
end
Run Code Online (Sandbox Code Playgroud)
控制器/ societes_controller
class SocietesController < ApplicationController
# I use basically the same code for the update method
def create
params[:societe][:type_taxes_ids] ||= []
@societe = Societe.new( societe_params )
@societe.user_id = current_user.id
if @societe.save
add_types_taxes_to_societe
redirect_to societes_path
else
load_resources
render :new
end
end
private
def add_types_taxes_to_societe …Run Code Online (Sandbox Code Playgroud) 我有一个与Section模型相关的Post模型,这取决于工作的额外条件:
<?php
class Post extends Base
{
public function section()
{
return $this->belongsTo('App\Models\Section', 'id_cat')->where('website', $this->website);
}
}
Run Code Online (Sandbox Code Playgroud)
当我想检索帖子并获取它的关联部分时,可以按照以下方式进行操作:
$post = Post::first();
echo $post->section->name; // Output the section's name
Run Code Online (Sandbox Code Playgroud)
但是,当尝试使用急切的负载获取该部分时:
Post::with(['section'])->chunk(1000, function ($posts) {
echo $post->section->name;
});
Run Code Online (Sandbox Code Playgroud)
Laravel抛出以下异常:
PHP error: Trying to get property of non-object
Run Code Online (Sandbox Code Playgroud)
当我对上述热切的加载查询返回的Post对象进行调试时,我注意到该section关系为null。请注意,如果我从belongsTo关联中删除条件,则它工作正常。
你们有什么想法为什么会发生吗?
我有一个AuthGuard负责检测用户是否登录的服务。如果没有登录,我会将用户重定向到我们的 oauth 提供程序 URL。
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { environment } from './../../environments/environment';
import { Session } from './../core/security/session.service';
@Injectable()
export class AuthGuard implements CanActivate {
/**
* Class constructor.
* @constructor
*
* @param {Session} - Instance of session.
*/
constructor(private session: Session) {}
/**
* Method to implements from CanActivate interface.
* Check if a user is authenticated.
*
* @return {boolean}
*/
canActivate(): boolean {
if …Run Code Online (Sandbox Code Playgroud) 我有一个使用sum functionnality的问题.所以基本上,我有2个表,paiment_type和账单.在账单表中,我有一个外键名称fk_paiement_type,它让我知道这个特定账单使用了什么样的票据.
打印统计数据时,我这样做是为了获得总数:
SELECT
pt.name,
SUM(f.total_ttc) AS total_mode
FROM
bills AS f
INNER JOIN paiement_type AS pt
ON pt.id = f.fk_paiement_type
WHERE (
f.created BETWEEN '2013-01-10'
AND '2013-01-10'
)
AND (
f.type LIKE 'facture'
OR f.type LIKE ''
)
GROUP BY f.fk_paiement_type
Run Code Online (Sandbox Code Playgroud)
这段代码工作得很好,但我实际上有3个不同的paiement类型,有时只有2个在白天使用,如果它不存在我仍然想要显示它.
编辑:我已经尝试使用IFNULL功能,但它没有用.来自账单表的fk_paiement_type有时仅返回与paiement_type表匹配的2个值.我想我的问题来自这里:
INNER JOIN paiement_type AS pt ON pt.id = f.fk_paiement_type
Run Code Online (Sandbox Code Playgroud)
任何的想法?
编辑2:
我的表结构如下:
**Bills Table**
id (int),
fk_paiement_type (int),
ref (varchar),
fk_client (int),
tss (double),
total_ttc (double),
type (varchar), …Run Code Online (Sandbox Code Playgroud) 我有一个表,其中有一个名为date. 在执行 POST 以插入新行时,从客户端(浏览器)发送的日期看起来2015-11-20T14:30:00+10:00实际上是正确的日期和时区。
但是,在 Postgres 中,此日期已插入为2015-11-20 04:30:00.000000,如您所见,与上面的完全不同。我知道问题与时区有关。但我似乎无法找到解决办法。
有关信息,我已经配置了我的应用时区:
class Application < Rails::Application
config.time_zone = 'Brisbane'
end
Run Code Online (Sandbox Code Playgroud)
想法?
我实际上是在尝试安装和使用Rails的Sunspot gem(https://github.com/sunspot/sunspot).到目前为止,这就是我所做的.我添加了依赖项Gemfile:
gem 'sunspot_rails'
gem 'sunspot_solr'
Run Code Online (Sandbox Code Playgroud)
然后我运行bundle并使用创建配置文件rails generate sunspot_rails:install.到现在为止还挺好.
但是,当我试图运行时bundle exec rake sunspot:solr:start,我面临以下错误:
rake aborted!
Don't know how to build task 'sunspot:solr:start'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task_manager.rb:49:in `[]'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:148:in `invoke_task'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/project/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/project/.rbenv/versions/2.0.0-p247/bin/rake:23:in `load'
/Users/project/.rbenv/versions/2.0.0-p247/bin/rake:23:in `<main>'
Run Code Online (Sandbox Code Playgroud)
我实际上使用的是Rails 4和Ruby 2.0.0.有没有人已经面临同样的问题或知道解决方法?
非常感谢您的帮助
laravel ×2
testing ×2
angular ×1
angularjs ×1
belongs-to ×1
config ×1
csrf ×1
datetime ×1
devise ×1
email ×1
interceptor ×1
jasmine ×1
mysql ×1
namespaces ×1
pdo ×1
php ×1
postgresql ×1
response ×1
sum ×1
sunspot ×1
timezone ×1
token ×1
validation ×1