这就是我正在使用的.令牌不一定要被猜测,它更像是一个短网址标识符,而不是其他任何东西,我想保持简短.我已经按照我在网上找到的一些例子,如果发生碰撞,我认为下面的代码会重新创建令牌,但我不确定.不过,我很想看到更好的建议,因为边缘感觉有点粗糙.
def self.create_token
random_number = SecureRandom.hex(3)
"1X#{random_number}"
while Tracker.find_by_token("1X#{random_number}") != nil
random_number = SecureRandom.hex(3)
"1X#{random_number}"
end
"1X#{random_number}"
end
Run Code Online (Sandbox Code Playgroud)
我的令牌的数据库列是一个唯一的索引,我也在validates_uniqueness_of :token
模型上使用,但因为这些是根据用户在应用程序中的操作自动批量创建的(他们下订单并购买令牌,基本上),它是让应用程序抛出错误是不可行的.
我猜,我也可以减少碰撞的几率,在最后添加另一个字符串,根据时间或类似的东西生成的东西,但我不希望令牌太长.
我正在尝试根据数据库中的布尔值发送多封电子邮件.该应用程序是一个简单的计划应用程序,用户可以将他们的班次标记为"replacement_needed",这应该向所有请求接收这些电子邮件的用户发送电子邮件.麻烦的是,它似乎只发送到一封电子邮件.这是我目前的代码:
def request_replacement(shift)
@shift = shift
@user = shift.user
@recipients = User.where(:replacement_emails => true).all
@url = root_url
@recipients.each do |r|
@name = r.fname
mail(:to => r.email,
:subject => "A replacement clerk has been requested")
end
end
Run Code Online (Sandbox Code Playgroud) 我希望在我的应用程序中执行一些特定于移动设备的布局,并且一直在研究检测移动浏览器和提供移动特定布局的方法.
我碰到这个传来:http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens
但是使用一系列关键字对我来说似乎有点脆弱.如果关键字改变怎么办?任何人遇到任何宝石或插件或更多的未来证明策略?
我正在使用HTTParty来提取Facebook用户的书籍列表,但我在解析响应时遇到问题:
Facebook以这种方式返回数据:
{
"data": [
{
"name": "Title",
"category": "Book",
"id": "21192118877902",
"created_time": "2011-11-11T20:50:47+0000"
},
{
"name": "Title 2",
"category": "Book",
"id": "1886126860176",
"created_time": "2011-11-05T02:35:56+0000"
},
Run Code Online (Sandbox Code Playgroud)
HTTParty将其解析为ruby对象.我尝试过这样的事情(ret
响应在哪里)ret.parsed_response
并返回数据数组,但实际访问里面的项目会返回一个找不到方法的错误.
这是HTTParty实际返回的示例:
#<HTTParty::Response:0x7fd0d378c188 @parsed_response={"data"=>[{"name"=>"Title", "category"=>"Book", "id"=>"21192111877902", "created_time"=>"2011-11-11T20:50:47+0000"}, {"name"=>"Title 2", "category"=>"Book", "id"=>"1886126860176", "created_time"=>"2011-11-05T02:35:56+0000"}, {"name"=>"Thought Patterns", "category"=>"Book", "id"=>"109129539157186", "created_time"=>"2011-10-27T00:00:16+0000"},
Run Code Online (Sandbox Code Playgroud) 我将数据导出到rails中的CSV文件和我的某些字段中,当我在Excel中打开时,我遇到了类似的字符编码问题:
didn’t
Run Code Online (Sandbox Code Playgroud)
我从一个例子中借用了这段代码,我假设编码已关闭.知道应该是什么吗?
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{filename}.csv"
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算产品的平均净价.我的产品型号中有:total_sold和:total_net_revenue.在方法中进行直接划分似乎总是导致0.我使用BigDecimal,因为我认为这是问题...但是使用我最新的代码迭代,当答案出现时,我仍然会得到零小数.
def avg_price
BigDecimal(total_sold.to_s) / (BigDecimal(total_net_revenue.to_s) / 100)
end
Run Code Online (Sandbox Code Playgroud)
净收入是美分,这就是我除以100的原因.有人可以指出我做错了什么或应该做什么?
我尝试过以下方法:
def next_seven_days
today = Date.today
(today .. today + 7).each { |date| puts date }
end
Run Code Online (Sandbox Code Playgroud)
但这只是给了我第一个也是最后一个日期.我无法弄清楚如何在两者之间获得所有这些.
我试图按照这里的例子:http://www.whynotwiki.com/Ruby_/_Dates_and_times
我有以下表单代码:
<input data-color="orange" id="vote_color_id_2" name="color" type="radio" value="2" />Orange
<input data-color="blue" id="vote_color_id_3" name="color" type="radio" value="3" />Blue
<input data-color="green" id="vote_color_id_4" name="color" type="radio" value="4" />Green
Run Code Online (Sandbox Code Playgroud)
我在rails中使用Coffee脚本,现在我只是想提醒数据属性的值data-color
.
这是我的咖啡脚本
jQuery ->
$("input[name='color']").change ->
color = this.data()
alert color.color
Run Code Online (Sandbox Code Playgroud)
编译的jQuery看起来像这样:
(function() {
jQuery(function() {
return $("input[name='color']").change(function() {
var color;
color = this.data();
return alert(color.color);
});
});
}).call(this);
Run Code Online (Sandbox Code Playgroud)
但我一直收到这个错误?
Uncaught TypeError: Object #<HTMLInputElement> has no method 'data'
Run Code Online (Sandbox Code Playgroud) 我总是在我的一个模型中使用行Model.count
并且对性能有点担心,因为最终,这个模型会变得非常大.有没有办法使用counter_cache
w:o:belongs_to关系?还是另一种性能友好的计算行数的方法?我想要制作另一个模型,只是我存储这样的计算但不确定这是最好的方法.
我的控制器中有以下内容:
def create
@board = Board.find(session[:board])
@greeting = @board.Greetings.build(params[:greeting])
respond_to do |format|
if @greeting.save
format.js { render :action => "success" }
else
format.js { render :action => "failure" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的rspec硒测试中,我想为电路板设置会话.但似乎我做不到
describe "greeting creation" do
before(:each) do
@board = Factory(:board)
session[:board] = @board.id
end
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
Failure/Error: session[:board] = @board.id
NoMethodError:
undefined method `session' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
如何设置会话以使此测试有效?