如何从ActiveRecord对象获取类名?
我有:
result = User.find(1)
Run Code Online (Sandbox Code Playgroud)
我试过了:
result.class
# => User(id: integer, name: string ...)
result.to_s
# => #<User:0x3d07cdc>"
Run Code Online (Sandbox Code Playgroud)
我只需要一个字符串中的类名(User在本例中).有没有办法呢?
我知道这是非常基本的,但我搜索了Rails和Ruby的文档,我找不到它.
这是对这个问题的更通用的重构(消除了Rails的特定部分)
我不确定如何在RESTful Web应用程序中的资源上实现分页.假设我有一个被调用的资源products,您认为以下哪个是最好的方法,以及为什么:
例如.http://application/products?page=2&sort_by=date&sort_how=asc
这里的问题是我不能使用整页缓存,并且URL也不是很干净且易于记忆.
例如.http://application/products/page/2?sort_by=date&sort_how=asc
在这种情况下,看到的问题是它http://application/products/pages/1不是一个独特的资源,因为使用sort_by=price可以产生完全不同的结果,我仍然不能使用页面缓存.
例如.http://application/products/by-date/page/2
我个人认为使用这种方法没有问题,但是有人警告我这不是一个好的方法(他没有给出理由,所以如果你知道为什么不推荐,请告诉我)
任何建议,意见和批评都非常受欢迎.谢谢.
是否可以使用POST方法重定向?
或者应该使用GET重定向?
用于此的是在电子商务站点的订购过程的最后步骤中,将数据发送到支付处理器,而不为用户引入额外的步骤.
我是一个习惯于mootools链接选择器的方式的人,我似乎无法找到如何在jQuery中做同样的事情.假设我在变量中有一个select元素selectObj.我需要的是获得该选择中的最后一个选项.在mootools我会做的事情如下:
var option = $(selectObj).getElement('nth-child(last)')
Run Code Online (Sandbox Code Playgroud)
我可以做类似的事情,或者option在jQuery 中获得最后的方法是什么?
PS.我知道父>子选择器,但我不能真正使用它,因为我不知道使用了什么选择器来获取select.我只有结果元素.
处理以下类型情况的推荐方法是什么:
假设我有一个名为的模型Cart,它与模型具有1-1关系,Person并且具有相同的PK(用户的id).
在index我的方法中,我cart_controller想检查Cart当前用户是否存在.如果我这样做Cart.find(the_user_id)并且购物车不存在RecordNotFound则会引发异常.
我看到两种解决方法:
begin
@cart = Cart.find(the_user_id)
#more code here
rescue ActiveRecord::RecordNotFound
#the cart is empty message
end
Run Code Online (Sandbox Code Playgroud)
if Cart.exists?(the_user_id)
@cart = Cart.find(the_user_id)
#more code here
else
#the cart is empty message
end
Run Code Online (Sandbox Code Playgroud)
从我(有限的)关于exeption处理的知识我知道不建议以这种方式使用异常,但每次都值得进行额外的查询吗?
是否可以使用辅助方法在创建<option>的末尾添加?<select>collection_select
现在我有
f.collection_select(:category_id , @categories, :id, :name, {:prompt => 'Please select a category'})
Run Code Online (Sandbox Code Playgroud)
产生
<select id="product_category_id" name="product[category_id]">
<option value="">Please select a category</option>
<option value="7">category one</option>
<option value="8">category 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我想要的是什么
<select id="product_category_id" name="product[category_id]">
<option value="">Please select a category</option>
<option value="7">category one</option>
<option value="8">category 2</option>
<option value="new">..or create a new one</option>
</select>
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我应该循环遍历集合并手动生成选项?
我正在使用cocoa框架帮助编写一个用objective-c编写的小应用程序,我遇到了多线程问题.如果有人可以帮我提供一些关于如何从主线程终止辅助(工作者)线程的指导,我真的很感激吗?
- (IBAction)startWorking:(id)sender {
[NSThread detachNewThreadSelector:@selector(threadMain:) toTarget:self withObject:nil];
}
- (void)threadMain
{
// do a lot of boring, time consuming I/O here..
}
- (IBAction)stop:(id)sender {
// what now?
}
Run Code Online (Sandbox Code Playgroud)
我在apple的文档上找到了一些东西,但是这个例子中缺少的是runloop输入源改变exitNow值的部分.
此外,我不会在我的应用程序中使用很多线程,所以我更喜欢一个简单的解决方案(开销较少)而不是一个能够轻松管理多个线程的更复杂的解决方案,但产生更多开销(例如使用锁定)也许(?)而不是runloops)
提前致谢
不能获得<div class="fieldWithErrors">包含验证错误的包装的arround选择标记是否正常?我个人认为没有理由为什么选择标签应该与其他表格标签(输入,textarea)区别对待.
我确实得到了该字段的错误error_messages_for和error_message_on方法.
PS.我已经改变了一点ActionView::Base.field_error_proc,以获得span标签而不是div,但这不是问题.
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
#if I puts html_tag here I only get the <input> tags
"<span class=\"fieldWithErrors\">#{html_tag}</span>"
}
Run Code Online (Sandbox Code Playgroud) 我试图覆盖autosave参数,因为我认为它无法完成.
我把它has_shipping_address从模型转移Order到ShippingAddress模型,现在我有:
#the models..
class Order < ActiveRecord::Base
belongs_to :billing_address
belongs_to :shipping_address
accepts_nested_attributes_for :billing_address
accepts_nested_attributes_for :shipping_address, :reject_if => proc { |attributes| attributes["has_shipping_address"] != '1' }
def after_initialize
self.build_billing_address unless billing_address
self.build_shipping_address unless shipping_address
end
end
class ShippingAddress < OrderAddress
attr_accessor :has_shipping_address
end
class OrderAddress < ActiveRecord::Base
validates_presence_of :name
#more validations here..
end
#the view
<% form_for @order do |f| %>
#...
<% f.fields_for :shipping_address do |addr_f| %>
<%= addr_f.check_box :has_shipping_address %>
<%= …Run Code Online (Sandbox Code Playgroud) 有关如何在Rails应用程序中的资源上实现排序和分页并仍保持RESTful的任何提示?
如何以干净的方式映射页码和排序字段的参数?(我目前正在为我的应用中的每个资源使用map.resources:x)
我应该知道的任何其他性能问题?(例如缓存)
提前致谢.
编辑我将问题重新制定为更通用的问题,希望我能得到更多答案.我打算投票结束这个,因为它不再相关.感谢insane-dreamer和vrish88的答案.
activerecord ×2
pagination ×2
rest ×2
ruby ×2
sorting ×2
actionview ×1
cocoa ×1
exception ×1
html ×1
javascript ×1
jquery ×1
nested ×1
nested-forms ×1
objective-c ×1
redirect ×1
validation ×1