我在我的sinatra应用程序中使用内联haml模板.我有一个@@layout
像这样的部分:
#message
- if flash[:notice]
%section.notice= flash[:notice]
- if flash[:error]
%section.error= flash[:error]
Run Code Online (Sandbox Code Playgroud)
当我使用flash[:notice]
="你好!" 在路线中点击一个链接,下一页愉快地说"你好"在#message
div中.很棒.
所以这就是问题所在,我正在使用right.js为我的应用程序添加一些ajax的好处,机架式闪存的行为充其量是不一致的.
大多数情况下,你单击一个链接(.linkey
),一点点javascript拦截它并加载到#content
div(这部分也可以),然后重新加载'#message'div并显示前一个动作的flash ...下次单击链接时...大约80%的时间,其余时间没有显示任何内容.
这是我的js:
"a.linkey".onClick(function(event) {
event.stop();
$('content').load( [ "/", this.get('id'), ].join("") );
$('message').load( '/message' );
});
Run Code Online (Sandbox Code Playgroud)
我希望是这样的:
1)点击链接
2)链接目标(/ foo)被加载到 #content
3)#message
重新加载消息(来自route flash[:notice]
="bar")
4)#content
现在显示/foo
并 #message
显示"bar"
我也尝试了这个,$('message').load( '/message' );
但要么加载任何东西#message
或填充#message
"/ message"(字符串不是内容).
我想知道这里发生了什么?是机架式闪存还是right.js?或者是其他东西?如果需要的话,我可以提供更多的代码,但除了基本框架之外,我刚刚开始这个项目.
好吧,这让我发疯了.我已经阅读了协会的文章和例子,并试图在拉斯三天内解决这个问题,我厌倦了这让我感到愚蠢,所以......
如何设置与DataMapper的关联?
(我正在使用带有SQLite3的带有Sinatra的DM.对于具有多个值等的单个表,一切都很好.当我开始尝试将它们关联起来时,我开始收到错误.)
假设我有一个装满苹果树的果园.每棵树都有很多苹果.每个Apple都有很多种子.因此每棵树通过其苹果有很多种子
require 'sinatra'
require 'datamapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/orchard.db")
# Trees in the orchard.
class Tree
include DataMapper::Resource
property :id, Serial
has n, :apples
has n, :seeds, :through => :apples
end
# Apples on a Tree.
class Apple
include DataMapper::Resource
property :id, Serial
belongs_to :tree
belongs_to :seed
end
# Seeds in an Apple
class Seed
include DataMapper::Resource
property :id, Serial
has n, :apple
has n, :tree, :through => apple
end
DataMapper.finalize.auto_upgrade!
Run Code Online (Sandbox Code Playgroud)
那是对的吗?我尝试运行时遇到各种错误.大多数沿着无效关联的行或者不能创建值为NULL的列NULL等.我不理解这种关系?
此外,一旦我有了工作模型,我该如何从中获取信息?
如果有3棵树:
Tree.all.count
=> 3
Run Code Online (Sandbox Code Playgroud)
如果有2个苹果:
Apple.all …
Run Code Online (Sandbox Code Playgroud) 有人可以向我解释这里发生了什么吗?
这是一个我放在一起展示你们最新情况的例子:
class Person
include DataMapper::Resource
property :id, Serial
property :type, Discriminator
property :name, String
property :age, Integer
end
class Male < Person
end
class Father < Male
property :job, String
end
class Son < Male
end
class Female < Person
end
class Mother < Female
property :favorite_song, String
end
class Daughter < Female
end
DataMapper.auto_upgrade!
Run Code Online (Sandbox Code Playgroud)
如果我打电话给Person.all
我:
Person.all
=> [#<Son @id=1 @type=Son @name="Mike" @age=23 @status=nil>,
#<Son @id=2 @type=Son @name="Carlos" @age=12 @status=nil>,
#<Father @id=3 @type=Father @name="Robert" @age=55 @job=<not loaded>>, …
Run Code Online (Sandbox Code Playgroud) 为什么:
code3_x = []
level = 7
(level + 2).times do |i|
# This is more what I want:
# code3_x[i] << i
# This works to demonstrate:
code3_x << i
end
Run Code Online (Sandbox Code Playgroud)
返回:
=> 9
Run Code Online (Sandbox Code Playgroud)
为什么不?
=> [0,1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
我创建一个数组作为code3_x
&我有一个值x
(用i
条件选择的数字替换第二个,只是i
在示例中重用作为占位符)我想x
在code3_x中的特定索引处插入.
注意:
我正在尝试将以下javascript翻译为ruby, 这只是一个更大功能的剪辑
if ... conditions ...{
code3_x[i] =2;
mod_x -= h_pow;
}else...
Run Code Online (Sandbox Code Playgroud)
更新: 这是一个链接到我试图在ruby中重新实现的整个javascript函数的要点. https://gist.github.com/therocketforever/d1dca656f4579bc5baf3