Tag: 的Ruby on 轨道

运行rails控制台时加载错误

我使用rails 4.1和ruby 2.1.1 一切正常,但是当我运行rails console时,我得到了这个错误 > rails console Loading development environment (Rails 4.1.0) load error: /home/andreas/.rvm/rubies/ruby-2.1.1/.irbrc NoMethodError: undefined method `split’ for nil:NilClass /home/andreas/.rvm/scripts/irbrc.rb:41:in `’ 错误后控制台打开并可以使用。 这是.irbrc文件中的第41行和周围环境。 39 # Calculate the ruby string. 40 rvm_ruby_string = ENV[“rvm_ruby_string”] || 41 (ENV[‘GEM_HOME’] && ENV[‘GEM_HOME’].split(/\//).last.split(/@/).first) || 42 (“#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}” rescue nil) || 43 (RUBY_DESCRIPTION.split(” “)[1].sub(‘p’, ‘-p’) rescue nil ) || […]

如何获得Rails构建和fields_for只创建一个新记录而不包括现有记录?

我使用build , fields_for和accepts_nested_attributes_for在与新注册相同的表单上创建新的注册注释(有许多注册注释)。 大。 问题:在现有注册的编辑表单上,我想要创建另一个新的注册注释,但我不希望看到每个现有注册注释的字段。 我有这个 class Registration < ActiveRecord::Base attr_accessible :foo, :bar, :registration_notes_attributes has_many :registration_notes accepts_nested_attributes_for :registration_notes end 还有这个 class RegistrationsController < ApplicationController def edit @registration = Registration.find(params[:id]) @registration.registration_notes.build end end 在我看来我这样做: 它正在为新的注册记录(好)创建一个空白文本区域,并为该注册创建每个现有的注册记录(不,谢谢)。 有没有办法只为该注册创建一个新注释并保留现有注释?

无法更新has_one关联的嵌套模型表单

我尝试为has_one关联创建一个嵌套的模型表单。 (我正在使用Rails 4) 在我的用户和地址模型中,我有以下内容: class User < ActiveRecord::Base has_one :address accepts_nested_attributes_for :address end class Address < ActiveRecord::Base belongs_to :user end 我的用户控制器: class UsersController < ApplicationController . . . def edit @user = User.find(params[:id]) @user.build_address if @user.address.nil? end def update @user = User.find(params[:id]) if @user.update(params.require(:user).permit(:user_name, address_attributes: [:street])) flash[:success] = "Profile updated successfully" sign_in @user redirect_to @user […]

在没有模型的情况下在Rails中validation

我有一个允许用户向电子邮件发送消息的表单,我想为其添加validation。 我没有这个模型,只有一个控制器。 我应该如何在Rails中执行此操作? 我正在考虑在控制器中进行validation,并使用flash对象向用户显示错误。 有更好的方法吗?

同时计数和分组

我有一个带有以下架构的Mail模型: t.string “mail” t.integer “country” t.boolean “validated” t.datetime “created_at” t.datetime “updated_at” 我想找到数据库中的前5个国家,所以我继续打字 @top5 = Mail.find(:all,:group => ‘country’,:conditions => [ “validated = ?” , “t” ], :limit => 5 ) 这将告诉我团体(我需要一个订单,我不知道怎么写) @top5 = Mail.count(:all,:group => ‘country’,:conditions => [ “validated = ?” , “t” ], :limit => 5 ) 这将告诉我每组中有多少邮件 我想知道我是否可以一次性分组和计算

如何使用资产管道在element div中添加style =“background-image?

在assets/images/default/user文件夹中,我存储了一个image banner.png 。 如何在以下background-image图像中直接添加此background-image ?

未定义的方法`merge’for 2:Fixnum

我收到以下错误 未定义的方法`merge’for 2:Fixnum 代码 f.check_box “page[group_ids][]”, g.id 拜托,建议解决方案。 谢谢

在现有模型上设计迁移

我正在从Authlogic迁移到Devise。 更新: 设计的迁移尝试重新创建表用户,所以我改变了,你可以在下面看到create_table到change_table并删除最后的表以删除我添加的内容 问题是当我运行rake时我得到一个错误。 这是我在运行rake时遇到的错误。 == DeviseCreateUsers: migrating ============================================== — change_table(:users) rake aborted! An error has occurred, this and all later migrations canceled: SQLite3::SQLException: duplicate column name: email: ALTER TABLE “users” ADD “email” varchar(255) DEFAULT ” NOT NULL 这是迁移 class DeviseCreateUsers false t.recoverable t.rememberable t.trackable # t.confirmable # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both […]

如何在rails上的ruby中创建facebook样式的新闻源?

我试图将对象融合成一个流。 我创建了一个名为Newsfeed的新模型和控制器。 我有它所以新闻源has_many:消息和has_many:图像和图像和消息belongs_to:newsfeed。 我正在设置控制器,我有: def index @messages = Messages.all @images = Images.all @feed = ? end 我如何将它们融合在一起,以便它们可以从两者中拉出来并按每个需要单独格式化时按时间顺序排列? 非常感谢任何帮助,谢谢。

Rails fields_for:child_index选项说明

我一直在尝试使用许多嵌套模型创建一个复杂的表单,并使其动态化。 现在我发现使用accepts_nested_attributes_for创建嵌套模型并不困难,但如果有多个嵌套模型,那么使嵌套模型嵌套和动态似乎是不可能的。 我遇到了http://github.com/ryanb/complex-form-examples/blob/master/app/helpers/application_helper.rb ,非常优雅。 有人能否对13号线和16号线有所了解? 13 form_builder.object.class.reflect_on_association(method).klass.new 和 16 form_builder.fields_for(method, options[:object], :child_index => “new_#{method}”) do |f| 从直觉来看,第13行实例化了一个新对象,但为什么必须这么多方法调用呢? 我找不到第16行的:child_index选项的任何文档。创建表单时,非常大的数字用作新模型的索引,而现有模型则按其id编制索引。 这是如何运作的?