在我的一些控制器中,我有一个before_filter来检查用户是否已登录?用于CRUD操作.
application.rb中
def logged_in?
unless current_user
redirect_to root_path
end
end
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
Run Code Online (Sandbox Code Playgroud)
但是现在我的功能测试失败了,因为它重定向到root.所以我需要一种方法来模拟会话已经创建,但我尝试过的任何东西都没有.这是我现在所拥有的,测试几乎忽略了它:
test_helper.rb中
class ActionController::TestCase
setup :activate_authlogic
end
Run Code Online (Sandbox Code Playgroud)
posts_controller_test.rb
class PostsControllerTest < ActionController::TestCase
setup do
UserSession.create(:username => "dmix", :password => "12345")
end
test "should get new" do
get :new
assert_response :success
end
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我有两个Postgres数据库.在一个我有两个表,每个表有大约8,000,000行,并且其中任何一个的计数大约需要一秒钟.在另一个数据库中,也是Postgres,有些表是1,000,000行,计数需要10秒,一个表大约有6,000,000行,计数需要3分钟才能运行.哪些因素决定了这需要多长时间?它们位于不同的计算机上,但需要更长时间的数据库位于更快的计算机上.
我已经读过一般来说postgres计数是如何缓慢的,但这对我来说似乎很奇怪.我无法真正使用解决方法,因为我正在使用django,它在管理中进行了计数,这将永远耗尽并使其难以使用.
任何有关此信息都会有所帮助.
我想让一个yaml对象引用另一个,如下所示:
intro: "Hello, dear user."
registration: $intro Thanks for registering!
new_message: $intro You have a new message!
Run Code Online (Sandbox Code Playgroud)
上面的语法只是它可能如何工作的一个例子(它也是它在这个cpan模块中的工作方式.)
我正在使用标准的ruby yaml解析器.
这可能吗?
如果url参数是urlencoded,rails会为我解码,还是我必须自己调用CGI :: unencode?
(我问,因为我看到的是我认为不一致的行为,并且可能是rails或rspec中的错误,但是想先在这里要求进行健全性检查)
在我的应用程序中,我有
VeryUniqueModule::Foo
# and…
VeryUniqueModule::Bar
Run Code Online (Sandbox Code Playgroud)
Foo并且Bar每个都是为不同的服务.我的应用程序的一部分必须动态地确定要引用的模块,它可以这样做:
def service_api
# @relevant_object.service is a string that is either 'Foo' or 'Bar'
VeryUniqueModule.const_get(@relevant_object.service)
end
Run Code Online (Sandbox Code Playgroud)
稍后会详细介绍.
我刚刚更新了一个库,现在它有自己的顶级Foo类(这是一个糟糕的设计).现在,当我尝试调用时@relevant_object.service_api::A_CONSTANT,我的应用程序抱怨图书馆 Foo没有A_CONSTANT.
回到service_api上面 - 我以为那const_get是回归班级本身.事实上,我知道它是.如果我在irb所有内容中按照预期启动它- 返回值是类本身,我可以调用类中的东西.所以…
A_CONSTANT的Class对象service_api,而不是我正在使用的字符串eval或类似的那些时髦的东西 - 应该没有任何命名空间问题,我直接指的是一个对象!service_api该如何修复以便它将返回,嗯,"完整路径"?我想使用RESTful控制器删除多个相同类型的对象.
我能想到的最简单的事情是让destroy动作期望以逗号分隔的对象id列表来销毁.
有没有更优雅的方式来做到这一点?
如果我在irb中进行反向交互式搜索,然后输入一个控制字符(箭头键是我实际看到的唯一的那个).所以我从这个会话开始:
$ irb
>> print "hello"
hello
Run Code Online (Sandbox Code Playgroud)
然后进入^r,h
$ irb
>> print "hello"
hello
(reverse-i-search)`he': print "hello"
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.然后我点击了左箭头,我最终得到了这个:
$ irb
>> print "hello"
hello
>> print "[Dhello"
Run Code Online (Sandbox Code Playgroud)
随后使用箭头会使光标正常移动.
我似乎无法找到所有组件中完整的示例.我很难删除图片附件
类
class Product
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images
end
class ProductImage
belongs_to :product
has_attached_file :image #(etc)
end
Run Code Online (Sandbox Code Playgroud)视图
<%= semantic_form_for [:admin, @product], :html => {:multipart => true} do |f| %>
<%= f.inputs "Images" do %>
<%= f.semantic_fields_for :product_images do |product_image| %>
<% unless product_image.object.new_record? %>
<%= product_image.input :_destroy, :as => :boolean,
:label => image_tag(product_image.object.image.url(:thumb)) %>
<% else %>
<%= product_image.input :image, :as => :file, :name => "Add Image" %>
<% end %>
<% end %>
<% end …Run Code Online (Sandbox Code Playgroud)在我的rails应用程序中,我有一些这样的代码:
def foo
if object_bar_exists
raise "can't create bar twice!"
end
Bar.create
end
Run Code Online (Sandbox Code Playgroud)
这可以通过进入应用程序服务器的两个不同请求来调用.如果此代码同时由两个请求运行,并且它们同时运行if检查,则不会找到另一个bar,并且bar将创建2 秒.
为"酒吧集合"创建"互斥体"的最佳方法是什么?DB中的专用互斥表?
更新
我应该强调,我不能在这里使用内存互斥,因为并发是跨请求/进程而不是线程.
我想拥有独立的 .markdown 文件,然后将其包含在我的 haml 模板中。所以我想以某种方式在模板中包含——而不是渲染——一个外部文件。我希望父文件包含:markdown在其中,直接包含在其下方,然后 .markdown 文件只是纯降价。
或者:有没有办法只使用 markdown 作为 rails 模板语言(同样的方式我可以用 erb 或 haml 编写模板或部分,而 rails 只是想出来)?
ruby ×5
activerecord ×1
count ×1
formtastic ×1
haml ×1
irb ×1
ledit ×1
locking ×1
module ×1
namespaces ×1
paperclip ×1
postgresql ×1
rack ×1
readline ×1
rest ×1
rspec ×1
tdd ×1
transactions ×1
yaml ×1