我最近一直在环顾Rails并注意到有很多引用current_user.这只来自Devise吗?即使我使用Devise,我是否必须自己手动定义?是否有使用的先决条件current_user(如会话,用户等的存在)?
嗨,我遇到了postgres的麻烦.我不记得我的postgres密码,也不知道如何更改密码.我猜我应该更改一个月前设置的md5密码设置,但我不知道如何找到该文件并使用我的终端打开它.有人可以帮忙吗?
我是编程新手.现在我正在研究Ruby.据我所知,全局变量是在全局命名空间中定义的(所以在任何类或函数之外).我正在读一些东西,它说全局变量$在他们面前有一个标志.那是什么意思?这是否意味着,当我定义一个函数或类,并要参考我的全局变量(让我们说这是edmund = 123)我将不得不像这样引用它:$edmund?
所以:
edmund = 123
def my_function()
456 + $edmund
end
Run Code Online (Sandbox Code Playgroud)
也是类变量(以那些开头的@@变量@),如实例变量(),您可以通过调用它们来访问它们Class.classvariable?他们的目的是什么?
嗨,我一直在尝试安装Ruby gem包.gem install bundle收到此消息后,我按照说明进行操作,但在输入时仍然无效bundle -v.我也打字
which bundle 并收到
/Users/edmundmai/.rvm/bin/bundle
Run Code Online (Sandbox Code Playgroud)
所以它存在!! 那为什么它不起作用!! 神秘的$ PATH有什么问题吗?(我是个菜鸟).
这是我的.bash_profile:
PATH=$PATH:~/bin
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
Run Code Online (Sandbox Code Playgroud)
这是我的.bashrc:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Run Code Online (Sandbox Code Playgroud)
这是我的宝石环境:
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.24
- RUBY VERSION: 1.9.3 (2012-04-20 patchlevel 194) [x86_64-darwin11.3.1] …Run Code Online (Sandbox Code Playgroud) 嗨,我无法概念化何时使用:source以及何时使用:class更复杂的模型.
这里我有一个用户和朋友的例子.
class User < ActiveRecord::Base
...
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
end
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :user_id, :status
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => 'friend_id'
end
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么友谊:class_name …
嗨,我正在尝试将postgresql连接到我的rails项目.我正在学习测试,但由于postgresql错误导致密码不正确,我的测试没有运行:
Edmunds-MacBook-Pro:langexchange edmundmai$ rake test:units
rake aborted!
fe_sendauth: no password supplied
Run Code Online (Sandbox Code Playgroud)
我已经读完了,我的pg_hba.conf文件最初已经是这样的:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication edmundmai
Run Code Online (Sandbox Code Playgroud)
这是我的rails项目中的database.yml文件
default: &default
adapter: postgresql
encoding: utf8
pool: 5
username: edmundmai
password:
development:
<<: …Run Code Online (Sandbox Code Playgroud) 嗨我在heroku和github上有我的rails应用程序,我目前正在我的应用程序中使用邮件程序:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "myemail@gmail.com",
:password => "PasswordShouldGoHere",
:authentication => "plain",
:enable_starttls_auto => true
}
Run Code Online (Sandbox Code Playgroud)
我不希望我的电子邮件和密码在我的github帐户上可见,因为人们只能登录并窃取我的信息.但是,如果我输入了一个假密码,那么当邮件发送时,我的应用程序会在heroku上给我一个错误.我知道我可以先将真实的电子邮件和密码推送到heroku然后编辑它并将假密码放在我的github帐户上,但是有更好的方法吗?
我有复杂的模型/表格.我不想重复记录,所以我想合并具有相似属性的记录.如何使用before_save回调取消"保存"?这就是我的想法:
class ProductsColor < ActiveRecord::Base
before_save :check_for_similar_record
def check_for_similar_record
if ProductsColor.exist?(color_id: self.color_id, product_id: self.product_id)
# merge values with existing ProductsColor and stop self from saving
end
end
end
Run Code Online (Sandbox Code Playgroud) 它们在if/else/end声明中使用时是否相同?你平常都做什么?我想知道是否存在任何微妙的差异或边缘情况,object并且!object.nil?会有不同的反应.