我的应用程序使用的是Rails 3.0.4和Devise 1.1.7.
我正在寻找一种方法来阻止用户共享帐户,因为该应用程序是基于订阅的服务.我已经搜索了一个多星期,我仍然不知道如何实施解决方案.我希望有人已经实施了一个解决方案,可以指出我正确的方向.
解决方案(谢谢大家的答案和见解!)
在应用程序controller.rb中
before_filter :check_concurrent_session
def check_concurrent_session
if is_already_logged_in?
sign_out_and_redirect(current_user)
end
end
def is_already_logged_in?
current_user && !(session[:token] == current_user.login_token)
end
Run Code Online (Sandbox Code Playgroud)
在session_controller中重写Devise Sessions控制器:
skip_before_filter :check_concurrent_session
def create
super
set_login_token
end
private
def set_login_token
token = Devise.friendly_token
session[:token] = token
current_user.login_token = token
current_user.save
end
Run Code Online (Sandbox Code Playgroud)
在迁移AddLoginTokenToUsers
def self.up
change_table "users" do |t|
t.string "login_token"
end
end
def self.down
change_table "users" do |t|
t.remove "login_token"
end
end
Run Code Online (Sandbox Code Playgroud) 我知道如何在coffeescript中进行循环递增,例如:
CoffeeScript的:
for some in something
Run Code Online (Sandbox Code Playgroud)
生成的Javascript:
for (_i = 0, _len = something.length; _i < _len; _i++)
Run Code Online (Sandbox Code Playgroud)
如何在Coffeescript中创建类似于此的循环递减?
for (var i = something.length-1; i >= 0; i--)
Run Code Online (Sandbox Code Playgroud) 我一直在研究为使用rails-api的纯JSON rails 4.1应用程序实现facebook,twitter和email/password身份验证的最佳方法.rails应用程序没有视图,只是在Android和iphone应用程序混合本机应用程序之间提供JSON(它们使用离子框架,这是一个编译成本机应用程序的角度应用程序).
在过去,我已经实现了基本身份验证,其中auth_token通过头文件从客户端应用程序传递到Rails API,使用auth中内置的rails没有任何问题,但是从那时起我需要与Facebook和Twitter集成我正在寻找利用oauth的解决方案.
我已经研究了设计,但它似乎与纯粹的JSON api搭配得很好.同样,omniauth似乎依赖于视图和重定向.此外,opro和门卫似乎很适合作为你自己的oauth提供商,但我不知道他们如何处理像facebook和twitter这样的第三方.
人们如何在rails中为纯JSON API应用程序实现多提供程序oauth身份验证?
mobile ruby-on-rails facebook-oauth rails-api ruby-on-rails-4
我是rails的新手,所以这可能是一个基本问题.我正在尝试创建一个表单,用户可以一次创建3条记录.我希望用户只需单击一次提交按钮.我正在向我的评论模型提交名称,评论和评级.目前,只有最后一条记录输入数据库.
<%= form_for([@user,@review]) do |f| %>
<table>
<tr>
<td>Rank</td>
<td>Name</td>
<td>Comment</td>
</tr>
<tr>
<td>1</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "5" %>
</tr>
<tr>
<td>2</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "3" %>
</tr>
<tr>
<td>3</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "1" %>
</tr>
</table>
<div class="actions">
<%= f.submit "Create my top 3" %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
任何建议表示赞赏.谢谢.
由于Rails 4删除了页面缓存和动作缓存,我想知道什么是Rails 4方式缓存一个没有变量且在视图中只有html的动作?我应该在视图中片段缓存静态html吗?当没有模型将其设置为过期时,如何设置etag/fresh_when?我很难找到一个示例或约定来缓存什么应该是最容易缓存的页面.
需要注意的是,虽然视图完全是静态的,但页面仍然具有动态导航栏,具体取决于用户是否已登录.你如何处理这样的静态页面,而不采取行动缓存,因为它已被删除,并且约定已设置为不使用gem版本?
例:
class HomesController < ApplicationController
def index
end
end
Run Code Online (Sandbox Code Playgroud)
家/ index.html.erb
<div>A bunch of normal html tags with no erb</div>
Run Code Online (Sandbox Code Playgroud)
编辑: 基于@ severin的回答和我自己的研究,这是我到目前为止所提出的.
class HomesController < ApplicationController
def index
fresh_when(["some-identifier", current_user, flash])
end
end
Run Code Online (Sandbox Code Playgroud)
此外,我正在使用https://github.com/n8/bust_rails_etags重置部署后的所有etags,因为视图可能在部署之间发生了变化.我认为这很好地涵盖了etag,虽然我仍然很好奇是否会自动包含一些关于视图的标识符以及是否需要"some-idnetifier"?是否会出现问题,有时current_user和flash会为零?
现在关于片段缓存静态内容的第二点.我假设我做了:
cache "v1" do
all my html
end
Run Code Online (Sandbox Code Playgroud)
我必须记住在页面更改时始终更改缓存标识符,否则我的应用程序将提供过时的内容.任何方式自动化这个或已经由rails处理?只是缓存最后一次更新视图或更聪明的东西会很好,所以我不必跟踪我的静态内容何时被更改.
我正在尝试运行使用rspec的基本启动示例:http://rspec.info/.
当我在命令提示符下键入时
ruby bowling_spec.rb
我收到以下错误
测试
# bowling_spec.rb
require 'bowling'
describe Bowling, "#score" do
it "returns 0 for all gutter game" do
bowling = Bowling.new
20.times { bowling.hit(0) }
bowling.score.should == 0
end
end
Run Code Online (Sandbox Code Playgroud)
码
# bowling.rb
class Bowling
def hit(pins)
end
def score
0
end
end
Run Code Online (Sandbox Code Playgroud)
错误信息
internal:lib/rubygems/custom_require:29:in
require': no such file to load -- bowling (LoadError) from <internal:lib/rubygems/custom_require>:29:inrequire'from bowling_spec.rb:2:in`
我试图从https://github.com/raid5/paths_of_glory/tree/rails3安装gem
当我在自述文件中放入以下代码行时,如自述文件的步骤1中所指定:
gem 'paths_of_glory', :git => 'git://github.com/raid5/paths_of_glory.git', :branch => 'rails3'
Run Code Online (Sandbox Code Playgroud)
然后我运行"bundle install"并获得以下消息:
获取git://github.com/raid5/paths_of_glory.git C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/source.rb:559:in
':没有这样的文件或目录 - git clone"git://github.com/raid5/paths_of_glory .git""C:/Ruby192/lib/ruby/gems/1.9.1/cache/bundler/git/paths_of_glory-e6d58ab38 da51ed00031d8072c6aad8102babf9e" --bare --no-hardlinks(Errno :: ENOENT)来自C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/sourc e.rb:559:ingit' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/sourc e.rb:618:in缓存"
......(还有几行)
Run Code Online (Sandbox Code Playgroud)from C:/Ruby192/bin/bundle:19:in `load' from C:/Ruby192/bin/bundle:19:in `<main>'
知道出了什么问题吗?我很感激.我正在使用rails 3而我的操作系统是7号窗口.
我的测试失败的原因如下:
ReferenceError:无法在文件中找到变量:moving_canvas_context(第5行)
我理解测试失败的原因.它不理解变量,因为它是在单独的javascript文件中定义的.然而,它在全球宣布并且在现实中起作用.
如何为此clear_canvas函数编写茉莉花测试?
JavaScript Canvas_actions:
(function() {
window.Canvas_Actions = (function() {
function Canvas_Actions() {}
Canvas_Actions.prototype.clear_canvas = function() {
moving_canvas_context.clearRect(0, 0, moving_canvas.width, moving_canvas.height);
main_canvas_context.drawImage(window.background_image, 0, 0, main_canvas.width, main_canvas.height);
return window.canvas_objects = [];
};
return Canvas_Actions;
})();
}).call(this);
Run Code Online (Sandbox Code Playgroud)
Canvas_actions的Jasmine测试:
(function() {
describe('Canvas Actions', function() {
return describe('clear_canvas', function() {
return it('clears the canvases and deletes all objects', function() {
var actions;
jasmine.getFixtures().fixturesPath = "../spec/javascript/fixtures";
loadFixtures("canvas_fixture.html");
actions = new Canvas_Actions();
actions.clear_canvas();
return expect(canvas_objects).toEqual([]);
});
});
});
}).call(this);
Run Code Online (Sandbox Code Playgroud) 我正在寻找有关使用国际化的最佳实践的一些提示和建议.我一直在四处寻找,但我对我读到的内容并不满意.我读过的大部分文章都集中在使用I18n的yml文件,这在我的情况下是行不通的.
我目前有几个带英文文本的数据库表.其中一些表的文本字段长度只有几句,有些则是6段以上的文本.我想用西班牙语这些字段.
我目前正在考虑的方法是使用I18n-active记录gem并拥有1个翻译表,该应用程序将用于应用程序中的所有翻译
class CreateTranslations < ActiveRecord::Migration
def self.up
create_table :translations do |t|
t.string :locale
t.string :key
t.text :value
t.text :interpolations
t.boolean :is_proc, :default => false
t.timestamps
end
end
def self.down
drop_table :translations
end
end
Run Code Online (Sandbox Code Playgroud)
这是最好的方法吗?
一方面,所有翻译都将很好地存储在一个表中.另一方面,每次用户向数据库查询I18n内容时.应用程序将在原始表中查询密钥,然后查询转换表.另一个值得关注的是,翻译表将是巨大的,有一个庞大的行,因为它会被存储为应用程序(从章节标题[三言两语]文本的整个页面的一切所有的翻译量.
任何信息表示赞赏.谢谢
脚步:
在我的本地主机服务器上,我在位于太平洋时区的浏览器中选择时间 2013 年 1 月 18 日上午 10 点 - 上午 11 点。然后我在 heroku 上的生产服务器上选择完全相同的时间。
如您所见,保存到数据库的内容的输出是不同的。我希望生产作为本地主机运行,并在将时间存储为 UTC 时考虑时区。这是什么原因造成的?heroku 服务器在 EST 中,但这并没有考虑到这种行为。
控制器:
def create
puts params[:event][:starts_at]
@event = Event.create!(params[:event])
puts @event.starts_at
end
Run Code Online (Sandbox Code Playgroud)
输出本地主机:
2013 年 1 月 18 日星期五 10:00:00 GMT-0800(太平洋标准时间)
2013-01-18 18:00:00 UTC
输出 Heroku(生产):
2013 年 1 月 18 日星期五 10:00:00 GMT-0800(太平洋标准时间)
2013-01-18 10:00:00 UTC
架构:
t.datetime "starts_at"
Run Code Online (Sandbox Code Playgroud)
环境:
Ruby on Rails 3.2.11
红宝石 1.9.3
Postgres