Tag: ruby在轨道上3

ruby在轨道上如何处理NaN

我已经阅读了几篇关于NaN文章,但没有弄清楚如何在Ruby on Rails中处理它。 我想检查一个值,如果它是NaN我想用零(0)replace它。 我尝试了以下 logger.info(".is_a? Fixnum #{percent.is_a? Fixnum}") 当百分比有NaN它返回false。 我在logging仪上做了一些改变 logger.info("Fixnum #{percent.is_a? Fixnum} percent #{percent}") 产量 Fixnum false percent 94.44444444444444 Fixnum false percent NaN Fixnum false percent 87.0

在Ruby中validation电子邮件地址的最佳/简单方法是什么?

validation电子邮件地址(在服务器端)的最佳/简单的方法是什么?

如何在不触摸updated_at属性的情况下更新单个属性?

我怎样才能做到这一点? 试图创build2个方法,称为 def disable_timestamps ActiveRecord::Base.record_timestamps = false end def enable_timestamps ActiveRecord::Base.record_timestamps = true end 和更新方法本身: def increment_pagehit update_attribute(:pagehit, pagehit+1) end 打开和closures时间戳使用callback如: before_update :disable_timestamps, :only => :increment_pagehit after_update :enable_timestamps, :only => :increment_pagehit 但它没有更新任何东西,甚至没有更新所需的属性(pagehit)。 有什么build议? 我不想创build另一个表来计算页面访问量。

在rails erb中做什么<%==%>?

我最近看到这个,觉得很有意思。 但我真的不明白它是什么? 防爆。 我有一个Rails应用程序,我想引导一些JSON,所以我不必再提出第二个要求。 通常我会写这样的东西。 <%= raw @model.to_json %>或<%= @model.to_json.html_safe %> 我必须发送消息raw或html_safe或json将html转义,因此不正确parsing。 但是,这似乎也工作。 <%== @model.to_json %> 但我找不到任何文件。 有谁知道这是做什么? 即它是一样的调用html_safe或raw ? 还是有更多的呢?

资源和资源方法之间的区别

resource和resources方法之间的逻辑差别是什么? 这里有一些例子: resource :orders, :only => [:index, :create, :show] > rake routes orders POST /orders(.:format) orders#create GET /orders(.:format) orders#show resources :orders, :only => [:index, :create, :show] > rake routes orders GET /orders(.:format) orders#index POST /orders(.:format) orders#create order GET /orders/:id(.:format) orders#show resource :orders > rake routes orders POST /orders(.:format) orders#create new_orders GET /orders/new(.:format) orders#new edit_orders GET […]

parsingstring以添加到URL编码的URL

鉴于string: "Hello there world" 我怎样才能创build一个URL编码的string,如下所示: "Hello%20there%20world" 如果string还有其他符号,我也想知道该怎么做,如: "hello there: world, how are you" 最简单的方法是什么? 我正在parsing,然后build立一些代码。

devise一次限制每个用户一个会话

我的应用程序正在使用Rails 3.0.4和Devise 1.1.7。 我正在寻找一种方法来阻止用户共享帐户,因为该应用程序是基于订阅的服务。 我一直在寻找一个多星期,但我仍然不知道如何实施解决scheme。 我希望有人已经实施了一个解决scheme,并指出我在正确的方向。 解决scheme (谢谢大家的答复和见解!) 在应用程序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 在重写Devise Sessions控制器的session_controller中: 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 在迁移AddLoginTokenToUsers def self.up change_table "users" do […]

在rails中渲染json的最快方法是什么?

我正在优化我们的Rails应用程序中的一些慢事务,并且我看到了显着的JSON视图渲染时间: Rendered welcome/index.json.rabl (490.5ms) Completed 200 OK in 1174ms (Views: 479.6ms | ActiveRecord: 27.8ms) 假设API调用正在返回它需要返回的数据, 在轨中呈现JSON的最快方法是什么? 我们正在使用Rabl,因为它能够轻松地共享代码,但是我们并没有把它绑在上面。

如何暂时禁用Rack-Mini-Profiler?

我在rails中使用机架迷你探查器就好了,但是在一些编程会话中,特别是在处理许多不同的客户端代码时,它会阻碍。 (主要在我的客户端debugging工具networking图等) 我试图用一个之前的filter来closures它,也用来查看用户是否有权查看configuration文件,但“取消授权”似乎没有为我做任何事情。 这是我的代码称为之前的filter: def miniprofiler off = true if off || !current_user Rack::MiniProfiler.deauthorize_request return elsif current_user.role_symbols.include?(:view_page_profiles) Rack::MiniProfiler.authorize_request return end Rack::MiniProfiler.deauthorize_request end 我也知道有一个设置“Rack :: MiniProfiler.config.authorization_mode”,但我无法find可能的设置是什么文档,并没有看到它在代码中使用? 现在它告诉我:allow_all,但是:allow_none也不做任何事情。 即使我可以暂时在开发环境文件中设置一个值,然后重新启动服务器,也可以达到我的目的。

如何在redirect时显示Rails Flash通知?

我在Rails控制器中有以下代码: flash.now[:notice] = 'Successfully checked in' redirect_to check_in_path 然后在/ check_in视图中: <p id="notice"><%= notice %></p> 但是,通知不显示。 如果我不在控制器中redirect,则工作完美: flash.now[:notice] = 'Successfully checked in' render action: 'check_in' 我需要一个redirect,虽然…不只是一个渲染的行动。 redirect后可以收到Flash通知吗?