以下是M Hartl的Ruby on Rails教程中的一些代码.任何人都可以解释为什么实例变量(@user)是必要的,为什么不使用局部变量.此外,由于实例变量应该是类实例中的变量,哪个类是@user实例化的?
require 'spec_helper'
describe User do
before { @user = User.new(name: "Example User", email: "user@example.com") }
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
end
Run Code Online (Sandbox Code Playgroud) 虽然这听起来与您在此处找到的其他问题类似,但有一点点扭曲.我有两个目录,比如/ home/rails/Rake和/ home/rails/test_app.rails目录是我放置所有rails项目的地方.
在Rake中,我有一个Rakefile和一个create.rake文件.
这就是我的rakefile看起来像
namespace :setup do
desc "something"
task :init do
print "Name of the destination directory: "
name = STDIN.gets.strip
cp_r '.', "../#{name}/lib/tasks"
cd "../#{name}"
sh "rake setup:create"
end
end
Run Code Online (Sandbox Code Playgroud)
并创建.rake
namespace :setup do
desc "Install"
task :create do
sh 'git init'
#some other code
end
end
Run Code Online (Sandbox Code Playgroud)
它的作用是显而易见的.我想将Rake目录的内容复制到/ test_app/lib/tasks.然后将目录更改为test_app并运行setup:在install.rake文件中定义的create task,该文件现在放在test_app/lib/tasks中.这有效,但这是这样做的耙方式吗?任何人都可以给我一点点暗示它是如何完成的,Rake方式.
这是我使用invoke方法时得到的错误:
$ rake setup:init
Name of the destination directory:
testapp
cp -r . ../testapp/lib/tasks
cd ../testapp
rake aborted!
Don't know …Run Code Online (Sandbox Code Playgroud) 好吧,我正在使用"font-awesome-rails"gem.我非常习惯在Rails之外使用font-awesome,但我认为它在Rails社区中并不受欢迎.
安装后,它会使用该格式创建图标
<i class="nameoftheicon"> </i>
Run Code Online (Sandbox Code Playgroud)
我想把它用于我的网站徽标,它包含来自font-awesome和一些文本的图标.所以我尝试过:
<%= link_to "", root_path, class: "icon-puzzle-piece icon-2x" %>
<%= link_to "My site", root_path, id: 'logo' %>
Run Code Online (Sandbox Code Playgroud)
它有效,但是当我悬停时,它们就是两个不同的元素.
什么是在单个<a>标签下组合图像和文本的Rails方式.
是否有任何流行的Rails替代font-awesome?
这是我的架构.
Home = new Schema({
name: { type: String, required: true},
description: {type: String},
administrator: {type : mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
users: [{
_id: {type : mongoose.Schema.Types.ObjectId, ref: 'User'},
email: {type: String},
name: { type: String},
status: { type: Number}
}],
rooms: [Room]
});
module.exports = mongoose.model('Home', Home);
Run Code Online (Sandbox Code Playgroud)
如果我想在多个文档中找到特定用户,我可以这样做
Home.find({"users.email":"johnk@gmail.com"},{users:1})
这将返回所有带有users.email = johnk@gmail.com的房屋
但是用户的字段是一个数组.
"users" : [
{
"status" : 0,
"name" : "Yaknow",
"email" : "yaknow@gmai.com",
"_id" : ObjectId("5875a42ea469f40c684de385")
},
{
"status" : 1,
"name" : "johnk",
"email" …Run Code Online (Sandbox Code Playgroud) 这有点傻.我使用Rails作为我的网络应用程序.它使用如下语句从数据库中获取数据
<div class="show-content"> <%= @page.content %> </div>
Run Code Online (Sandbox Code Playgroud)
一个示例html输出相同.
<div class="show-content">
Migrations are a convenient way for you to alter your database in a structured and organized manner.
Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate.
Migrations also allow you to describe these transformations using Ruby.
</div>
Run Code Online (Sandbox Code Playgroud)
如您所见,它们由3个段落组成.但是当它们被显示时,新线(空格)被剥离,这不是我想要的.我应该创建一个帮助器还是有一个内置函数来做到这一点?