Tag: ruby

使用动态变量渲染部分rails

我试图根据用户所在的分类单元渲染部分内容。 在我的application.html.erb布局中,我有以下代码行: 在taxons控制器中,在show方法中,我有: @taxon_id = params[:id].split(‘/’).first 在分类单位#show我有: 当我运行它时,我得到一个SyntaxError。 但是在分类中#show如果我只是输入: 没有’= true’然后页面呈现,输出’@enable_women’。 所以我知道它正在获取正确的变量,我只需要将该变量设置为true。 我错过了什么? 非常感谢。

Rails:为什么时间数据类型从1/1/2000开始

我有一个专栏,我只想存储时间(小时,分钟,秒)。 例如,这是一个迁移: add_column :products, :start_hour, :time 当我插入数据库时​​, start_hour始终以1/1/2000开头,例如: 2000-01-01 01:19:00 +0700 。 请解释一下为什么。

时间戳无法保存 – postgresql

我有以下代码片段: class Foo include DataMapper::Resource property :id, Serial property :timestamp, DateTime end 我只想将当前时间转换为ms: class Time def to_ms (self.to_f * 1000.0).to_i end end def current_time time = Time.now return time.to_ms end time = current_time # => 1352633569151 但是当我要用上面的时间戳保存Foo时,它就无法保存到数据库中而且我没有收到任何错误消息。 foo = Foo.new foo.timestamp = time foo.save 任何的想法?

如何根据Rails中的值更改HTML输出

我想尝试简单地添加一个HTML类,如果返回的数据库值是一个特定的数字。 这是我尝试过的: <li > <li > <li > <li > 但是没有任何反应,也没有产生错误。 此外,我在控制台中运行AR代码并返回正确的值。 特别: given_size = Client.where(user_id: listing.client_id).first.dogsize 对此最好的方法是什么?

在Rails上自动翻译DB中的动态内容

我有一个大的rails应用程序,它使用定制的CMS,将某些页面部分存储在数据库中。 这个应用程序还有一个translate(字符串)函数,用于在另一个DB表||中查找字符串 如果找不到谷歌翻译API。 现在这个设置很有效,除了一个小方面 – 当切换到另一种语言时,静态内容也必须被翻译,所以我们只是将内容克隆到不同的行。 我想要做的是实现在模板中使用的某种标记,并使用相同的帮助程序,使普通翻译从静态模板中调整字符串。 我有用户输入标签,例如[翻译[mary有一只小羊羔]],当页面被渲染时,帮助者会启动并从数据库或谷歌翻译API中提取翻译。 帮助器已经工作得非常好,有memcache,CRUD管理页面来管理字符串等等。 所以基本上: This is some random text that won’t be translated And the following piece of text will be translated using the helper: [translate[mary had a little lamb]] 是否有(安全)方法来实现这一目标? 非常感谢任何指针。 我们已经有几百页了,并且在这个应用程序上运行的每个站点克隆它们15次将是一场噩梦。 稍后编辑:内容在Q标题中称为动态,因为它来自数据库。 然而,对于最终用户来说,它是带有一些文本的静态HTML。 对不起,如果这引起任何混淆。

使用自定义function包装许多rails属性

我有许多具有属性的模型,我需要在读取它们时应用一些新的行为。 我对Ruby / Rails相当陌生,所以现在我只是为一个属性定义一个getter并在其上面应用我的function(类似于亵渎过滤的东西),但是我将会这样做很多对象/属性,并希望更清洁。 例如,对于Post对象的body属性,这就是我现在完成的方法: class Post < ActiveRecord::Base include Replaceable #… # A key point is that we want to keep the original content in the db def body profanity_filter(self[:body]) end end ……我的担忧看起来像这样: module Replaceable extend ActiveSupport::Concern def profanity_filter(content) # filter and update content… content end end 这很有效,我很高兴,除了现在我必须将它应用到整个应用程序的许多领域,我想要比在各处覆盖吸气剂更优雅的东西。 我调查了代表,以便我可以做类似的事情 delegate :body, :title, :etc, :to […]

CSV读取v / s从数据库读取的临时表,循环优化和活动记录使用情况。 ruby

文件的CSV解析非常慢,所以我试图直接将文件加载到数据库中的某个临时表中,然后执行如下计算: 之前就是这样,花了13分钟使用以下方法添加条目: CSV.foreach(fileName) do |line| completePath = line[0] num_of_bps = line[1] completePath = cluster_path+ ‘/’ + completePath inode = FileOrFolder.find_by_fullpath(completePath, :select=>”id”) metric_instance = MetricInstance.find(:first, :conditions=>[“file_or_folder_id = ? AND dataset_id = ?”, inode.id, dataset_id]) add_entry(metric_instance.id, num_of_bps, num_of_bp_tests) end def self.add_entry(metaid, num_of_bps, num_of_bp_tests) entry = Bp.new entry.metric_instance_id = metaid entry.num_of_bps = num_of_bps entry.num_of_bp_tests = num_of_bp_tests entry.save return […]

Rails,Polymorphic Association – 仅渲染关联实例

我正在尝试学习如何在我的Rails 5应用程序中使用多态关联。 我有一个名为Organization,Proposal和Package :: Bip的模型。 协会是: 组织 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 提案 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 套票:必必 belongs_to :ipable, :polymorphic => true, optional: true, inverse_of: :bip Package :: Bip可以与Organization或Proposal相关联。 我正在努力弄清楚如何在我的提案节目中展示仅属于提案的Package :: Bips,以及组织中的提案。 我的package :: bip表有以下两列: # ipable_id :integer # […]

ActionView :: Template ::错误(application.css未预编译)Rails

我在我的生产服务器下面: Started GET “/” for 101.160.190.206 at 2013-09-05 00:11:28 +1000 Processing by HomeController#index as HTML Rendered home/index.html.erb within layouts/application (0.5ms) Completed 500 Internal Server Error in 2ms ActionView::Template::Error (application.css isn’t precompiled): 4: 5: 6: <meta name="description" content="”> 7: “all” %> 8: 9: 10: app/views/layouts/application.html.erb:7:in `_app_views_layouts_application_html_erb__153052184121312410_16560580′ 我运行rake assets:precompile ,它似乎正确编译,因为我可以在资产文件中看到,但我仍然得到 We’re sorry, but something went wrong. 错误。 […]

Ruby on Rails:nil的未定义方法`call’:NilClass

这是它登陆的路线。 @object = Object.find(params[:id]) respond_to do |format| if @object.update_attributes(:status=>Object::PUBLISHED, :published_at => Time.now) format.html { redirect_to :action => :publish_detail } format.xml { head :ok } format.json { head :ok } #else # flash[:error] = ‘There was a problem updating your object.’ # format.html { redirect_to :action => “publish_detail” } # format.xml { render :xml => @object.errors, […]