我无法弄清楚如何使用该.where()方法来检索关联的模型数据.在此示例中,Projects belongs_to Users ...
class Project < ActiveRecord::Base
belongs_to :user
has_many :videos
end
class User < ActiveRecord::Base
has_many :projects
end
class ProjectsController < ApplicationController
def invite
@project = Project.includes([:user]).where( {:hashed_id=>params[:id]} ).first
end
end
Run Code Online (Sandbox Code Playgroud)
在App/views/projects/invite.html.erg中<%= debug( @project ) %>返回:
--- !ruby/object:Project
attributes:
id: 22
name: Some Project Name
belongs_to: 1
instructions: Bla bla bla
active: true
max_duration: 2
max_videos:
created_at: 2013-08-26 15:56:50.000000000 Z
updated_at: 2013-08-26 15:56:50.000000000 Z
hashed_id: '1377532589'
Run Code Online (Sandbox Code Playgroud)
不应该将相关的用户哈希/数组包含在此中吗?我知道我可以通过调用第二个find/ where(@project.user = User.where( …
我在使用phpunit v5.1.3的CakePHP v3.x上.我的单元测试似乎没有认识到我的灯具的存在.看来测试是从local(default)数据库连接读取的.
这是我的TestCase类:
namespace App\Test\TestCase\Model\Table;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* App\Model\Table\ScreensTable Test Case
*
* @property \App\Model\Table\ScreensTable ScreensTable
*/
class ScreensTableTest extends TestCase
{
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.screens'
];
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::exists('Screens') ? [] : ['className' => 'App\Model\Table\ScreensTable'];
$this->Screens = TableRegistry::get('Screens', $config);
}
public testSomethingHere(){
$newScreen = $this->Screens->newEntity(['who' => …Run Code Online (Sandbox Code Playgroud) 欢迎阅读第32,342,343页"为什么Internet Explorer会吸得这么多?" ...
我见过很多关于IE9上传文件的报道很多的报道.显然它有很多关于什么时候会起作用或不起作用的警告(如果有人有明确的清单,我很乐意看到它).但是,我看到的大多数问题/解决方案都与javascript有关,通常是jQuery表单插件或类似的东西.
我的表单不是通过AJAX提交的,文件输入字段不会被css隐藏或隐藏.然而,我每天都会从IE9上的用户那里获得几张支持票,试图提交表单并且"没有任何反应"(=表单提交.没有错误,但文件没有上传.)我没有得到一个投诉不同的浏览器,IE8甚至似乎工作(以及它曾经做过).
这是我表格的顶部.我错过了什么吗?
<form action="http://mysite.dev/account-settings/?open=resume" method="post" class="wpjb-form" enctype="multipart/form-data">
<input type="hidden" name="resume_form" value="resume_form" />
<fieldset class="wpjb-fieldset-default">
<input id="firstname" name="firstname" type="hidden" class="regular-text " value="John" />
<input id="lastname" name="lastname" type="hidden" class="regular-text " value="Henry" />
<input id="email" name="email" type="hidden" class="regular-text " value="john.henry@johnhenry.com" />
<div class="wpjb-element-input-checkbox wpjb-element-name-is_active">
<label class="wpjb-label">Show resume? </label>
<div class="wpjb-field">
<label for="is_active_1"><input type="checkbox" class="" name="is_active" id="is_active_1" value="1" checked="checked" /> Yes <small style="display:inline;">(Uncheck to hide your resume)</small></label>
</div>
</div>
<div class="wpjb-element-input-select-one wpjb-element-name-file">
<label class="wpjb-label">Upload a <i>new</i> resume …Run Code Online (Sandbox Code Playgroud) forms internet-explorer file-upload cross-browser internet-explorer-9
在我的Rails 4应用程序项目(模型)has_many视频(模型)中.我在视频模型中有一个命名范围:
scope :live, where( is_deleted: 0, sent_to_api: 1 )
Run Code Online (Sandbox Code Playgroud)
在我的一个项目视图中,我这样做(项目是Project的一个实例):
project.videos.live.size
Run Code Online (Sandbox Code Playgroud)
我期望获得的是该特定项目中的项目数量,但我获得了任何项目中的视频数量.这好像.live不是从一个子集返回.videos而是替换它.
我在这里解释说,将命名范围彼此链接应该与逻辑AND组合,但是当应用于"关联方法"[< - 不确定.videos在此上下文中的正确术语]时似乎没有发生.
这样做的正确方法是什么?
我是云托管新手......
我正在开发一个PHP Web应用程序,它作为"Cedar"应用程序在Heroku上托管.Heroku为他们所有的子域提供"背负式"SSL,所以我可以正常加载https://myapp.herokuapp.com.但我也可以加载http://myapp.herokuapp.com.我想通过将http请求重定向到强制SSL https.
通常,这很容易.我只想使用mod_rewrite如下:
RewriteCond %{HTTPS} != on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
但是这对HEROKU没有用!
看来SSL会在流量到达我的应用之前终止上游.因此%{HTTPS}从未满足条件,结果是重定向循环.我也试过以下,但也没有用:
RewriteCond %{SERVER_PORT} != 443 #<--also redirect loop
RewriteCond %{REQUEST_SCHEME} !https #<--also redirect loop
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何在上游终止时检测/重定向到HTTPS?
我想用AJAX上传文件.在过去,我通过使用神奇的jQuery表单插件实现了这一点,它工作得很好.目前我正在构建一个Rails应用程序,并试图做"Rails方式",所以我使用Form Helper和paperclip gem来添加文件附件.
该轨道文档警告说,表单助手不为AJAX文件上传的工作:
与其他表单不同,制作异步文件上传表单并不像使用remote:true提供form_for那么简单.使用Ajax表单,序列化是通过在浏览器内运行的JavaScript完成的,因为JavaScript无法从硬盘驱动器读取文件,所以无法上载文件.最常见的解决方法是使用不可见的iframe作为表单提交的目标.
很明显,没有现成的解决方案.所以我想知道最聪明的事情是什么.好像我有几个选择:
这三个似乎都有可能.我至少知道#3,特别是[其他一些宝石]部分.我发现了两个类似的问题(这个和这个),它提到了一个名为Uploadify的Pic-Upload分支,但这两个问题都是2年之久,处理Rails 2和3(而且上传还没有多年更新).所以考虑到变化有多大,我认为这确实是一个全新的问题:
在Rails 4中使用AJAX上传文件的最佳方法是什么?
我已经看到Bootstrap <= 2的一些黑客,但我正在使用v3,我想制作一个水平行的链接,在行/容器中居中.这是我的标记:
<div class="footer row">
<div class="col-12">
<ul id="menu-main" class="nav navbar-nav">
<li class="text-center">
<%= link_to "Home", root_path %>
</li>
<li class="text-center">
<%= link_to "About", root_path %>
</li>
<li class="text-center">
<%= link_to "Help", root_path %>
</li>
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用CakePHP v2.1 +中的事件系统
它看起来非常强大,但文档有些模糊.触发事件看起来很简单,但我不知道如何注册相应的监听器来监听事件.相关部分在这里,它提供以下示例代码:
App::uses('CakeEventListener', 'Event');
class UserStatistic implements CakeEventListener {
public function implementedEvents() {
return array(
'Model.Order.afterPlace' => 'updateBuyStatistic',
);
}
public function updateBuyStatistic($event) {
// Code to update statistics
}
}
// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Order->getEventManager()->attach($statistics);
Run Code Online (Sandbox Code Playgroud)
但它没有说明这个代码应该驻留在哪里.在特定的控制器内?在app控制器里面?
如果它是相关的,那么监听器将成为我正在编写的插件的一部分.
更新: 这听起来像一种流行的方法是将监听器注册码放在插件的bootstrap.php文件中.但是,我无法弄清楚如何从那里调用getEventManager(),因为应用程序的控制器类等不可用.
更新2: 我还被告知听众可以住在模特里面.
更新3: 最后一些牵引力!以下代码将在MyPlugin/Config/bootstrap.php中成功记录事件
App::uses('CakeEventManager', 'Event');
App::uses('CakeEventListener', 'Event');
class LegacyWsatListener implements CakeEventListener {
public function implementedEvents() {
return array(
'Controller.Attempt.complete' => 'handleLegacyWsat',
);
} …Run Code Online (Sandbox Code Playgroud) 我正在按照插件测试设置/安装说明进行操作.我wp scaffold plugin-tests my-plugin跑了 但是在我尝试运行的下一步,bash bin/install-wp-tests.sh wordpress_test root '' localhost latest我得到以下错误:
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (61)'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'
Run Code Online (Sandbox Code Playgroud)
我的本地Wordpress站点正在运行MAMP(正在运行).我不确定这是否与安装脚本相关,因为我认为它正在创建一个临时数据库来运行测试......如果它使用内置的OSX mysql或MAMP的MySQL是否重要?
这是来自的输出 wp --info
$ ./vendor/wp-cli/wp-cli/bin/wp --info
PHP binary: /Applications/MAMP/bin/php/php5.6.10/bin/php
PHP version: 5.6.10
php.ini used: /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
WP-CLI root dir: /Applications/MAMP/htdocs/pipeline/wp-content/plugins/wp-github-pipeline/vendor/wp-cli/wp-cli
WP-CLI global config:
WP-CLI …Run Code Online (Sandbox Code Playgroud) 基本问题:我希望我的应用程序即使在用户离开时也可以拨打关于授权用户的Facebook图形API.
例如,我希望用户(A)授权应用程序,然后我希望用户(B)能够使用该应用程序查看有关用户(A)的朋友的信息.具体来说:"工作"字段.是的,我请求那些扩展权限(user_work_history,friends_work_history,等).目前,我的应用程序可以访问登录用户的朋友工作历史记录,但不能访问应用程序其他用户的任何朋友的工作历史记录.
这就是我所知道的:
offline_access到scope参数是旧方法,它不再起作用.我需要交换一个普通的访问令牌来获取新的扩展令牌.FB文档说:
https://graph.facebook.com/oauth/access_token?
client_id = APP_ID&client_secret = APP_SECRET&grant_type = fb_exchange_token&fb_exchange_token = EXISTING_ACCESS_TOKEN
这是我不知道的(我希望你能告诉我): 我如何使用Facebook PHP SDK获得扩展(又名"长寿")访问令牌?目前,我的代码如下所示:
$facebook->getAccessToken();
Run Code Online (Sandbox Code Playgroud)
有这样的事吗?:
$facebook->getExtendedAccessToken();
Run Code Online (Sandbox Code Playgroud)
如果没有,这是我应该做的吗?
$accessToken = $facebook->getAccessToken();
$extendedAccessToken = file_get_contents("https://graph.facebook.com/oauth/access_token?
client_id={$appId}&
client_secret={$secret}&
grant_type=fb_exchange_token&
fb_exchange_token={$accessToken}"
);
Run Code Online (Sandbox Code Playgroud)
我试过了它并没有用.我收到此错误:
Warning: file_get_contents(https://graph.facebook.com/oauth/access_token? client_id=#######& client_secret=#########& grant_type=fb_exchange_token& fb_exchange_token=##########) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /...
Run Code Online (Sandbox Code Playgroud)
如果我切换到FQL而不是图形API,它会有什么不同吗?我已多次阅读Facebook文档,但PHP sdk没有完整记录,我找不到任何有关如何工作的示例.
ruby ×3
cakephp ×2
file-upload ×2
.htaccess ×1
ajax ×1
associations ×1
bash ×1
cakephp-2.0 ×1
cakephp-2.1 ×1
cakephp-3.0 ×1
css ×1
events ×1
facebook ×1
forms ×1
heroku ×1
mamp ×1
mod-rewrite ×1
php ×1
phpunit ×1
ssl ×1
unit-testing ×1
wordpress ×1
wp-cli ×1