小编and*_*ndi的帖子

我如何获得Ruby类的名称?

如何从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的文档,我找不到它.

ruby ruby-on-rails rails-activerecord

375
推荐指数
4
解决办法
24万
查看次数

REST Web应用程序中的分页

这是对这个问题的更通用的重构(消除了Rails的特定部分)

我不确定如何在RESTful Web应用程序中的资源上实现分页.假设我有一个被调用的资源products,您认为以下哪个是最好的方法,以及为什么:

1.仅使用查询字符串

例如.http://application/products?page=2&sort_by=date&sort_how=asc
这里的问题是我不能使用整页缓存,并且URL也不是很干净且易于记忆.

2.使用页面作为资源和查询字符串进行排序

例如.http://application/products/page/2?sort_by=date&sort_how=asc
在这种情况下,看到的问题是它http://application/products/pages/1不是一个独特的资源,因为使用sort_by=price可以产生完全不同的结果,我仍然不能使用页面缓存.

3.使用页面作为资源和URL段进行排序

例如.http://application/products/by-date/page/2
我个人认为使用这种方法没有问题,但是有人警告我这不是一个好的方法(他没有给出理由,所以如果你知道为什么不推荐,请告诉我)

任何建议,意见和批评都非常受欢迎.谢谢.

sorting rest pagination

325
推荐指数
9
解决办法
6万
查看次数

redirect_to在rails中使用POST

是否可以使用POST方法重定向?
或者应该使用GET重定向?

用于此的是在电子商务站点的订购过程的最后步骤中,将数据发送到支付处理器,而不为用户引入额外的步骤.

ruby redirect ruby-on-rails

60
推荐指数
4
解决办法
5万
查看次数

在jQuery中链接选择器

我是一个习惯于mootools链接选择器的方式的人,我似乎无法找到如何在jQuery中做同样的事情.假设我在变量中有一个select元素selectObj.我需要的是获得该选择中的最后一个选项.在mootools我会做的事情如下:

var option = $(selectObj).getElement('nth-child(last)')
Run Code Online (Sandbox Code Playgroud)

我可以做类似的事情,或者option在jQuery 中获得最后的方法是什么?

PS.我知道父>子选择器,但我不能真正使用它,因为我不知道使用了什么选择器来获取select.我只有结果元素.

javascript jquery css-selectors method-chaining

27
推荐指数
2
解决办法
4万
查看次数

ActiveRecord的#存在?或从ActiveRecord :: RecordNotFound救援

处理以下类型情况的推荐方法是什么:

假设我有一个名为的模型Cart,它与模型具有1-1关系,Person并且具有相同的PK(用户的id).

index我的方法中,我cart_controller想检查Cart当前用户是否存在.如果我这样做Cart.find(the_user_id)并且购物车不存在RecordNotFound则会引发异常.
我看到两种解决方法:

1.从例外救援

 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)

2.使用ActiveRecord#存在?方法

 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处理的知识我知道不建议以这种方式使用异常,但每次都值得进行额外的查询吗?

activerecord ruby-on-rails exception

17
推荐指数
2
解决办法
2万
查看次数

Rails的collection_select帮助方法和最后的"创建项目"选项

是否可以使用辅助方法在创建<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)

这是可能的还是我应该循环遍历集合并手动生成选项?

html ruby-on-rails

12
推荐指数
1
解决办法
5484
查看次数

从主线程终止辅助线程(可可)

我正在使用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)

提前致谢

cocoa multithreading objective-c

6
推荐指数
1
解决办法
6890
查看次数

Rails验证和'fieldWithErrors'包装选择标记

不能获得<div class="fieldWithErrors">包含验证错误的包装的arround选择标记是否正常?我个人认为没有理由为什么选择标签应该与其他表格标签(输入,textarea)区别对待.

确实得到了该字段的错误error_messages_forerror_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)

validation ruby-on-rails actionview field-with-errors

6
推荐指数
1
解决办法
5877
查看次数

rails accepts_nested_attributes_for:reject_if无效

我试图覆盖autosave参数,因为我认为它无法完成.
我把它has_shipping_address从模型转移OrderShippingAddress模型,现在我有:

#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)

activerecord nested ruby-on-rails nested-forms

5
推荐指数
1
解决办法
5249
查看次数

Rails RESTful应用程序中的分页和排序

有关如何在Rails应用程序中的资源上实现排序和分页并仍保持RESTful的任何提示?

如何以干净的方式映射页码和排序字段的参数?(我目前正在为我的应用中的每个资源使用map.resources:x)

我应该知道的任何其他性能问题?(例如缓存)

提前致谢.

编辑我将问题重新制定为更通用的问题,希望我能得到更多答案.我打算投票结束这个,因为它不再相关.感谢insane-dreamer和vrish88的答案.

sorting rest pagination ruby-on-rails

1
推荐指数
1
解决办法
6184
查看次数