在设计中,我如何访问用户的身份验证令牌.当ajax调用启动用户会话时,我需要能够从用户那里获取身份验证令牌.我尝试过在我的模型中添加:token_authenticable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Run Code Online (Sandbox Code Playgroud)
还添加了以下名称的属性:authentication_token和:authenticity_token到attr_accessible.但是每当我尝试用user.auth调用authentication_token或者authenticity_token时,我会收到像这样的错误
NoMethodError: undefined method `authentication_token' for #<User:0x000001016b1140>
Run Code Online (Sandbox Code Playgroud)
对不起,我需要通过我的ajax调用来获取此令牌,(我工作的最后一个项目我有一个令人难以置信的BOOTLEG解决方案,其中设备几乎被撕掉了应用程序,我只需要这个该死的令牌)
brendan lims关于设置sms_fu gem的说明
sms_fu = SMSFu::Client.configure(:delivery => :action_mailer)
Run Code Online (Sandbox Code Playgroud)
要么
sms_fu = SMSFu::Client.configure(:delivery => :pony, :pony_config => { :via => :sendmail })
Run Code Online (Sandbox Code Playgroud)
要么
PONY_CONFIG = {
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => 'username',
:password => 'password',
:authentication => :plain,
:enable_starttls_auto => true,
:domain => "localhost.localdomain"
}}
sms_fu = SMSFu::Client.configure(:delivery => :pony, :pony_config => PONY_CONFIG)
Run Code Online (Sandbox Code Playgroud)
我尝试了第一个(假设默认情况下设置了actionmailer,也许这是错误的).然后第二个(安装小马和邮寄的东西,以确保它的工作,这很好)和最后一个.上面没有出现任何错误.说明在这个设置之后,这样的命令应该起作用
sms_fu.deliver("5558675309","at&t","message")
Run Code Online (Sandbox Code Playgroud)
在所有设置中我都会收到此错误
NameError: uninitialized constant RAILS_ROOT
from /Users/name/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:102:in `template_directory'
from /Users/name/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:55:in `config_yaml'
from /Users/name/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:65:in `from_address'
from /Users/name/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:41:in `deliver' …Run Code Online (Sandbox Code Playgroud) 我试图覆盖设计,以发送邮件来激活用户.在注册控制器的创建方法中我有这个
urlEmail = resource.email.sub('@','-')
Pony.mail(
:to => resource.email,
:from => "noreply@mysite.com",
:subject => "Confirm Account",
:headers => { 'Content-Type' => 'text/html' },
:body => ("<h1>Welcome To My Awesome Site</h1>
<p>follow this link to create your account</p>
<p>http://localhost:3000/confirm-me/stuff?=" + resource.confirmhash.to_s + "/" + urlEmail.to_s + "</p>") )
Run Code Online (Sandbox Code Playgroud)
该URL导致激活用户的方法.这是否是确认帐户的好方法.问题是当Pony.mail(...)运行时我得到这个错误
uninitialized constant RegistrationsController::Pony
Run Code Online (Sandbox Code Playgroud)
我安装了pony,Pony.mail在控制台中工作.我也尝试在控制器文件的顶部使用require'pony',但我得到了
no such file to load -- pony
Run Code Online (Sandbox Code Playgroud)
我需要做些什么来完成这项工作.
我需要在我的rails应用程序中进行加密和解密.我试图使用ezcrypto,但每当我做解密时,我得到这个错误.
OpenSSL::Cipher::CipherError in ProfilesController#show
wrong final block length
Run Code Online (Sandbox Code Playgroud)
需要更改什么才能阻止此错误.我尝试使用另一种openssl实现(从我的模型中调用的方法)
def encrypt_attr(unencrypted)
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::SHA1.hexdigest('pass')
e = c.update(unencrypted)
e << c.final
return e
end
def decrypt_attr(encrypted_attr)
if encrypted_attr != ""
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.decrypt
c.key = Digest::SHA1.hexdigest('pass')
d = c.update(encrypted_attr)
d << c.final
return d
end
end
Run Code Online (Sandbox Code Playgroud)
它会在解密时抛出完全相同的错误.我该怎么做加密和解密而不是这个openssl错误.
以下哪种方式可以更有效地完成此任务.我想循环遍历模型,检查由每个model_id关联的整数值组成的列表是否大于0.如果是,则将相应的模型放入模型列表中.
@models = Model.find(:all).collect{|m| m }.reject{ |i| modellist[i.id] < 1 }
Run Code Online (Sandbox Code Playgroud)
或者像这样
finalModels = []
Model.find_each do |model|
if modellist[model.id] > 0 #edited
#if modellist[model.id] != 0
finalModels.push( model )
end
end
@models = finalModels
Run Code Online (Sandbox Code Playgroud)
我倾向于第二种方法,但我不确定.也许对.collect和.reject如何有效地了解它是多么有效.
我的模型叫做Picture.modellist(或pList)包含与此类似的数据.
[nil,nil,nil,3,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,
nil,nil,7,nil,nil,nil,0,nil,nil,nil,0,0,nil,nil,1,3]
Run Code Online (Sandbox Code Playgroud)
我的pList的索引号对应于此的图片ID.所以我需要找到pList [picture id]大于0的图片.
二手Benoit Garrets的回答.我要做的是确保pList是由pList = Hash.new而不是pList = []声明的.我使用的确切查询是
@pictures = Picture.find(pList.select {|k, v| v > 0}.keys)
Run Code Online (Sandbox Code Playgroud) 我需要一个脚本才能访问我的模型.我发现了一篇关于这个建议的帖子
require "#{ENV['RAILS.root']}/config/environment.rb"
Run Code Online (Sandbox Code Playgroud)
在我的脚本的顶部.然后我可以运行ruby script/my_script.rb来运行它.但这给了我错误
/Users/my_name/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- /config/environment.rb (LoadError)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么