Tag:

我怎样才能使那个ActiveRecord无表模型成为可能?

我正在使用Ruby on Rails 3,我想对一个ActiveRecord无表模型进行初始化。 在我的模型中,我有: class Account < ActiveRecord::Base # The following ActiveRecord Tableless Model statement is from http://codetunes.com/2008/07/20/tableless-models-in-rails/ def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end attr_reader :id, :firstname, :lastname, def initialize(attributes = {}) @id = attributes[:id] @firstname […]

在实施“副作用”元编程时加载类的麻烦

我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3-p125。 方案如下: 我实现了一个acts_as_customizable插件,我有一个Article模型类“充当可自定义”。 该插件通过元编程, 将一个名为customize 的方法添加到Comment模型类中( 注意 :由于这个原因,该插件具有副作用,因为操作了Article对象范围之外的Comment对象)。 鉴于这种情况,当我(重新)启动服务器并运行@comment.customize我得到错误NoMethodError – undefined method ‘customize’ for # 。 但是,当我(重新)启动服务器并运行以下代码时,所有工作都按预期工作: Article @comment.customize 如果我理解“邪恶”部分,上面的代码是有效的,因为在运行customize方法之前对Article类的“简单”调用使得它触发acts_as_customizable方法,该方法又将(通过元编程) customize方法添加到Comment类。 我该如何避免“邪​​恶”?

在Rails帮助器中的类中使用link_to

我有一个使用下面结构的rails帮助器,但是当我使用它时,我得到了消息 undefined method ‘link_to’ 帮助者安排如下: module MyHelper class Facet def render_for_search link_to(“Value”, params) end end class FacetList attr_accessor :facets def initialize #Create facets end def render_for_search result = “” facets.each do |facet| result << facet.render_for_search end result end end end

在rails app中使用自定义function

我试图按照以下答案中的简短示例来使用rails中的自定义函数: http://stackoverflow.com/questions/2879679/where-to-put-code-snippets-in-rails 在lib / math.rb中的math.rb中 module Math class << self def cube_it(num) num*3 end end end 在rails控制台中我试过了 include Math Math.cube_it(2) 但我得到错误: NoMethodError: undefined method ‘cube_it’ for Math:module

Ruby循环和类; 将字符串拆分为数组并再次返回字符串

Ruby新手在这里处理循环与类。 我应该创建一个方法,它将采用一个字符串并在每个单词的末尾添加感叹号(通过使其成为一个带有.split的数组)并再次将“惊呼”的单词作为字符串加入。 我已经在这里待了两个小时,并决定我应该寻求帮助。 我有一些想法,但我不断提出NoMethod错误。 以下是对我有意义的方法之一,但当然,它不起作用。 我还在最后添加了规格。 class StringModifier attr_accessor :string def initialize(string) @string = string end def proclaim new_array = [] string.split.each do |word| new array = “#{word}!” new_array.join end new_array end end 眼镜 describe StringModifier do describe “#proclaim” do it “adds an exclamation mark after each word” do blitzkrieg_bop = StringModifier.new(“Hey ho let’s go”).proclaim […]

Ruby on Rails – 使用符号数组获取对象关联值

例如,有模型: class Activity < ActiveRecord::Base belongs_to :event end class Event < ActiveRecord::Base has_many :activities attr_accessible :foo end 我可以通过使用activity.event.foo(足够简单)来获取活动的事件foo。 但是我想创建一个generics函数,首先找出一个对象是否有belongs_to关联,然后通过belongs_to关联获取该对象的foo(假设所有对象都通过belongs_to关联有foo)? 到目前为止,我有以下内容给我一个反思: def get_foo(object) object.class.reflect_on_all_associations(:belongs_to).each do |belongs_to| return object.?????? end end 我可以通过belongs_to.klass(例如[Event])获取reflection类名的数组,或者通过belongs_to.name(例如[:event])获取belongs_to关联的符号数组。 考虑到从reflection中得到的东西,我如何获得对象的belongs_to的foo? 没有使用reflection,有没有更简单的方法来做到这一点? 我希望这很简单,我只想说明如何解决这个问题。 我也希望我有点清楚。 这是我的第一个Stack Overflow问题。

类中的实例变量未保存

我通过before_validation过滤我的PersonalInfo类的所有实例值,如下所示: before_validation :strip_tabs def strip_tabs self.instance_variables.map do |attr| value = self.instance_variable_get(attr) if value.present? && value.kind_of?(String) encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => ”, # Use a blank for those replacements :universal_newline => true # Always break lines with \n […]

Rails保留类名

我尝试创建一个名为“class级”的模型(如同一个gradleclass的学生),并遇到各种各样的问题。 在Rails中要避免哪些其他单词或类名? 我找到的一些链接: http://juicebar.wordpress.com/2007/05/30/reserved-words-in-rails/ http://railsforum.com/viewtopic.php?id=22242

将用户定义的类放在Rails中的位置

我正在尝试使用这个课程 http://robbyonrails.com/articles/2005/05/11/parsing-a-rss-feed 但我不确定将文件放在何处,以便它像助手一样运行。

未定义的方法`collect’代表nil:NilClass

有人可以帮我解决这个错误吗? post中的NoMethodError #create 显示C:/Users/User/Documents/website/app/views/posts/_form.html.erb,其中第24行引发: 未定义的方法`collect’代表nil:NilClass 提取的来源(第24行): 23: 24: 模板包含的跟踪:app / views / posts / new.html.erb 我的_form.html.erb { :multipart => true }) do |f| %> prohibited this post from being saved: 我的控制器: class PostsController < ApplicationController # GET /posts # GET /posts.json def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render […]