我有一个简单的设置User和UserProfile模型与User has_one :user_profile和UserProfile belongs_to :user.
但我无法理解Rails如何定义after_create回调的执行顺序并accepts_nested_attributes_for在我的模型中定义.让我们考虑这两种情况.
情况1:
class User < ActiveRecord::Base
has_one :user_profile
accepts_nested_attributes_for :user_profile
after_create :test_test
end
Run Code Online (Sandbox Code Playgroud)
现在,如果我通过控制台创建用户(也使用user_profile_attributes哈希),after_create则会在创建用户及其用户配置文件后触发回调.
案例2:
如果after_create放在顶部,
class User < ActiveRecord::Base
after_create :test_test
has_one :user_profile
accepts_nested_attributes_for :user_profile
end
Run Code Online (Sandbox Code Playgroud)
在创建用户之后但在创建用户配置文件之前触发回调.
这是它预期运作的方式.Rails在这里做什么?执行顺序是否仅由代码的顺序决定?
我在哪里开始深入研究或调试这个?
我的第一篇文章!
我必须获取最新的推文,因此不得不执行跨浏览器请求.当前的应用程序使用原型,但我对jquery有点熟悉.
所以,我开始在jquery中:
$.ajax('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
dataType: "jsonp",
success:function(data,text,xhqr){
$.each(data, function(i, item) {
console.log(item.text);
});
}
});
Run Code Online (Sandbox Code Playgroud)
我得到一个警告:
'Resource interpreted as Script but transferred with MIME type application/json.'
Run Code Online (Sandbox Code Playgroud)
但是,我确实看到了我的最后一条推文.精细.
因此,我决定在原型中做同样的事情,然后尝试消除警告,如果有的话.但是,即使在尝试了几个小时后,我也无处可去.
这是我最初在原型中提出的.我接下来发生了很多变化/改动,但都没有.
new Ajax.Request('http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?', {
contentType: "jsonp",
onSuccess:function(transport){
console.log(transport) ;
}
});
Run Code Online (Sandbox Code Playgroud)
请求成功但响应文本为nil /"".我在Firefox中没有错误,但在Chrome中错误是:
XMLHttpRequest cannot load http://twitter.com/status/user_timeline/apocalyptic_AB.json?count=1&callback=?. Origin http://localhost:4000 is not allowed by Access-Control-Allow-Origin.
Refused to get unsafe header "X-JSON"
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.谢谢.
我有一个标题费用.在python中使用xlwt,我成功生成了所需的excel.在创建Excel文件时,此列始终为空.
是否可以将费用列预先格式化为"货币"和"两位小数",这样当我在下载后手动在Excel文件的费用栏中写入时,23应该变为$ 23.00?
TubePlayer是一个实现YouTube Player API 的jQuery插件,允许我们为YouTube视频创建自己的控件和组件.该TubePlayer网站提供了有关如何开始使用它的提示.
但是,这对我不起作用.其网站上的此页面演示了快速入门指南.我完全遵循它指定的但不起作用.
视频播放正常,但不知何故控制视频的链接不起作用.
代码有什么问题吗?如果你想尝试一下,这里是代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script src="http://www.tikku.com/scripts/ui/tubeplayer/jQuery.tubeplayer.min.js"></script>
<script>
$(function(){
jQuery("#youtube-player-container").tubeplayer({
width: 600, // the width of the player
height: 450, // the height of the player
allowFullScreen: "true", // true by default, allow user to go full screen
initialVideo: "ylLzyHk54Z0", // the video that is loaded into the player
preferredQuality: "default",// preferred quality: default, small, medium, large, hd720
onPlay: function(id){}, // after the …Run Code Online (Sandbox Code Playgroud) javascript jquery jquery-plugins youtube-api youtube-javascript-api
我有两个模型公司和角色分别由has_many和belongs_to协会相关.我需要获得一个拥有正确数量角色的公司.
我想出来了
Company.joins(:roles).having("'count(roles.id) = ?', 3")
Run Code Online (Sandbox Code Playgroud)
但这不起作用.有没有积极的记录解决方案?
谢谢.
javascript ×2
jquery ×2
activerecord ×1
after-create ×1
ajax ×1
callback ×1
excel ×1
mysql ×1
prototypejs ×1
python ×1
twitter ×1
xlwt ×1
youtube-api ×1