Tag: ruby on rails

Rails.application.config.autoload_paths与标准Ruby require / require_relative有什么区别?

我在application.rb中看到以下配置: config.autoload_paths += %W(#{config.root}/app/models/custom_pack/base) config.autoload_paths += Dir[“#{config.root}/app/models/custom_pack/custom_container/**/”] config.autoload_paths += Dir[“#{config.root}/app/models/custom_pack/helpers/**/”] config.autoload_paths += Dir[“#{config.root}/app/models/custom_pack/extensions/**/”] 在autoload_paths中: Rails.application.config.autoload_paths.grep /custom/ => [“/Users/dviglione/projects/core/app/models/custom_pack/base”, “/Users/dviglione/projects/core/app/models/custom_pack/custom_container/”, “/Users/dviglione/projects/core/app/models/custom_pack/helpers/”, “/Users/dviglione/projects/core/app/models/custom_pack/extensions/”] 但是这些文件没有被加载。 因为我收到错误: `method_missing’: undefined method `create_element’ for # create_element方法是在应该加载的其中一个文件中定义的。 但是,当我使用require / require_relative时,它确实有效: # /initializers/my_initializer.rb require “custom_pack/base” # models/custom_pack/base.rb require_relative ‘custom_container/base’ require_relative ‘custom_container/parent’ require_relative ‘custom_container/child’ Dir[File.join(File.expand_path(“../helpers”, __FILE__), “*_helper.rb”)].each { |file| require file } Dir[File.join(File.expand_path(“../extensions”, __FILE__), […]

Ruby on Rails – 导入CSV文件

我正在遵循这个教程CSV-FILE-EXPORT-IMPORT-RAILS,但我做错了,因为当我试图创建我的对象未初始化的常量CuentaContablesController :: False时,我遇到了一个愚蠢的错误。 我可以毫无问题地阅读该文件,但这个错误令我头疼! 任何帮助将不胜感激! 在我的控制器中导入的方法(cuenta_contable_controller.rb)如下所示; class CuentaContablesController < ApplicationController …. def upload(params) logger.info "**File loaded***" infile = params[:file].read n, errs = 0, [] @archivo = [] SV.parse(infile) do |row| n += 1 # SKIP: header ie first row OR blank row next if n == 1 or row.join.blank? cuenta_contable = CuentaContable.build_from_csv(row) if cuenta_contable.valid? cuenta_contable.save […]

Rails 3.从数组中获取总数

我正在打印一个包含行总数的表格,但我也希望获得总列数。 以下代码不起作用,而不是总计,它只打印出最后一次迭代的值。 Totals

尝试使用实例化对象调用函数时没有方法错误

我有一个模型令牌,有三个字段user_id,product_id和unique_token 。在控制器中,我实例化了一个@token对象,其中包含从表单中收集的user_id和product_id值 。然后我用该对象调用save_with_payment函数,在我想要生成的函数中随机字符串3次并保存在unique_token字段中 。问题是self.tokens.create!( unique_token: Digest::SHA1.hexdigest(“random string”) )给我没有方法错误undefined method tokens我在这里做错了什么?为了阐明我想要完成的任务,我希望能够检索与user_find或product_id相关联的生成的unique_tokens列表,如User.find(1).tokens或Product.find(1).tokens 。模型关联是User has_many Tokens Product has_many Tokens 。 注意:unique_token字段最初来自Token模型,user_id和product_id只是ref主键。非常感谢! def create @token=Token.new(params[:token]) if @token.save_with_payment redirect_to :controller => “products”, :action => “index” else redirect_to :action => “new” end end class Token “usd”,card:stripe_card_token,:description => “Charge for bucks”) #self.stripe_customer_token = customer.id 3.times do self.tokens.create!(unique_token: Digest::SHA1.hexdigest(“random string”)) end save! end […]

ticket有可以发送消息的用户 – rails messaging

我不确定如何为私人消息构建路由。 当用户点击故障单时,它会显示故障单详细信息以及向用户发送有关故障单的消息的选项。 但是,新的消息路由看起来像users/:username/messages/new ,但它应该反映用户(发送者)正在向另一个用户(接收者)发送有关当前正在查看的故障单的消息。 如何使我的路线RESTful? 模型可以在这里找到: receiver_id not saving – rails messaging 编辑 我最终改变了我的routes.rb到: resources :users do resources :tickets do resources :messages end end resources :tickets 定义多个资源有什么不利之处吗?

使用fields_for – Rails创建多个记录

我有两个模型, task和list_items 。 task has_many list_items 。 但是我希望能够创建task和许多list_items如果用户想要所有的一个forms。 这是目前的样子: 形成 任务控制器 def new @task = @assignment.tasks.new @list_item = @task.list_items.build @all_list_items = [] end 基本上我要做的是创建多个列表项和一个创建任务。 但是,如果我提交表单,我现在收到此错误 错误 undefined method `description’ for []:Array 有人知道我做错了什么以及如何实现这个目标?

Rails应用程序中的项目/微博的CRUD所有权

我有一个简单的rails应用程序,用户可以在其中创建“项目”,但在列出所有项目的主索引页面上,每个“项目”旁边都有“显示,编辑和删除”链接。 我知道这是因为我使用脚手架来完成这些项目,但我想确保人们只能编辑他们创建的项目。 这个逻辑现在略高于我的头脑,就像我之前说过的那样,对于铁轨来说我是全新的。 用户控制器: class UsersController < ApplicationController def show @user = User.find_by_username(params[:id]) end def index @user = User.find(:all) end end 主项目视图: All Items Title Details Inquire “michaelomchenry@gmail.com”, :subject => “OverFlow Inquiry Regarding ” + item.title %> 商品型号: class Item { :maximum => 140 } belongs_to :user delegate :email, to: :user end

动态validates_length_of

我想尝试执行validates_length_of,但在运行时指定范围/最小值/最大值。 例如,我们有一个父模型: class Parent < ActiveRecord::Base has_many :children # with attributes min_length, max_length end 还有一个儿童模特: class Child < ActiveRecord::Base belongs_to :parent # with an attribute reference end 那么我想在Child课程中做的是: validate :reference_length def reference_length options = { :within => parent.min_length..parent.max_length } self.class.validates_length_of :reference, options end 但它不起作用,有没有办法做到这一点,而不做errors.add(:reference, message) if… ?

思考 – 狮身人面像指数最低

我有产品和价格模型,其中: class Product < AR::Base has_many :prices # there are several types of prices, eg for guests, users et.c. 我想按价格值对产品进行索引和分类,属于该产品和确切的price_type。 ThinkingSphinx::Index.define :product, with: :active_record do indexes name indexes k1c indexes catalogue_code indexes created_at, sortable: true indexes prices(:value), as: :price, sortable: true # TODO has :category_id has :brand_id has :kind_cd has :price_id end 在rake ts:rebuild I […]

如何修复level.rb使用:承诺的日子?

最初我有:committed日子工作得非常漂亮,但是在更改模型时,为了适应用户的检查能力,如果他错过了:committed一天我现在收到与以下代码相关的错误消息:committed : undefined method to_date for nil:NilClass Line#30 n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday } 此代码来自习惯模型: class Habit < ActiveRecord::Base belongs_to :user has_many :levels has_many :days, through: :levels #for being able to access Habit.find(*).days accepts_nested_attributes_for :levels, :days before_save :set_level acts_as_taggable serialize :committed, Array def evaluate(user) levels.each […]