Tag: ruby on rails 4.1

哪个create / create_record在ActiveRecord中消失了?

我有一个ActiveRecord的审计猴子补丁,适用于从2.x到4.0.2的所有版本的Rails,但不适用于ActiveRecord 4.1。 4.0的代码看起来像这样 module HLLAuditStamps def self.included(base) # create/update became create_record/update_record in Rails-4.0 base.alias_method_chain :create_record, :audit base.alias_method_chain :update_record, :audit private def create_record_with_audit set_audit_attributes . . . 这适用于4.0.2但在4.1.2中抛出此exception: create_record’ for class ActiveRecord :: Base的未定义方法create_record’ for class ‘(NameError) 如果我在rails-4.1.2项目中进入rails控制台并列出ActivRecord :: Base中的方法,我会看到: . . . – :count_by_sql – :create – :create! – :create_with – :current_scope . . . […]

Rails关联中的未知密钥

我有以下协会代码: has_many :rates_without_dimension, :as => :rateable, :class_name => “Rate”, :dependent => :destroy, :conditions => {:dimension => nil} has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater has_one :rate_average_without_dimension, :as => :cacheable, :class_name => “RatingCache”, :dependent => :destroy, :conditions => {:dimension => nil} dimensions.each do |dimension| has_many “#{dimension}_rates”, :dependent => :destroy, :conditions => {:dimension => dimension.to_s}, :class_name […]

带有Rails的Websockets(Puma) – WebSocket握手期间出错:意外的响应代码:200

我试图在我的Rails4.1应用程序中使用websocket 以下是一些相关的代码片段: 的Gemfile: gem ‘websocket-rails’ gem ‘puma’ development.rb config.middleware.delete Rack::Lock 我在本地启动服务器: bundle exec puma -p 3000 在chrome控制台中,我看到一个连接错误: new WebSocketRails(‘localhost:3000/post/hello’, true); WebSocket connection to ‘ws://localhost:3000/post/hello’ failed: Error during WebSocket handshake: Unexpected response code: 200 任何人都可以帮助我在Rails中本地使用Web套接字需要做什么? UPDATE1 当我在Unicorn服务器上运行时,我尝试按照我的Rails应用程序中不起作用的Websockets添加以下内容,但是在瘦服务器上运行但是它没有帮助 初始化/ eventmachine.rb Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive

创建我的用户时,参数数量错误(0表示1)

我知道有很多问题,但我无法找到答案。 所以,我构建了一个Rails 4 API,我尝试创建一个发布JSON请求的用户。 这里有一些我的代码: user.rb: class User “Position”, foreign_key: “position_current_id” has_many :positions searchkick locations: [“location”] def search_data attributes.merge location: [current_position.latitude, current_position.longitude] end devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end application_controller.rb class ApplicationController < ActionController::Base respond_to :json, :html protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format =~ %r{application/json} } end registrations_controller.rb class Api::RegistrationsController {:token […]

设计中未定义的局部变量或方法`resource_class’

我正在尝试添加一个名为username的属性来设计但rails在以下代码行中显示错误我正在使用rails 4.0.0并设计3 def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) } end 控制器应用 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_action :configure_permitted_parameters if :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) } end […]

使用.env文件在生产中设置SECRET_KEY_BASE

我在生产中的根文件夹中有一个.env文件。 此文件定义了在config/secrets.yml使用的SECRET_KEY_BASE 。 问题是我无法在config/secrets.yml .env之前加载我的.env文件。 我尝试过使用dotenv gem而没有成功。 关于如何在生产中做到这一点的任何想法? 我不想为生产服务器上的webmaster用户全局设置它。 应该只能由应用程序访问SECRET_KEY_BASE值。 我正在使用rails 4.1。

Rails 4.1.6会话ID

如何从请求对象获取会话ID。 下面的代码可以在Rails 3中用来获取会话ID。 但是这在Rails 4.1.6和Ruby 2.1.3中似乎不再起作用 request.session_options[:id]

Rails资产管道:如何创建自定义清单文件

我试图从application.js单独创建一个自定义清单javascript文件。 我从application.js获取代码并将其粘贴到我称为“other_manifest.js”的新文件中,并放在assets / javascrips目录中。 这是代码: // This is a manifest file that’ll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. // // It’s not advisable […]

Rails 4.1.2 – to_param转义斜杠(并打破app)

我在我的应用程序to_param使用创建自定义URL(此自定义路径包含斜杠): class Machine < ActiveRecord::Base def to_param MachinePrettyPath.show_path(self, cut_model_text: true) end end 问题是,由于Rails 4.1.2行为已更改且Rails不允许在URL中使用斜杠(使用自定义URL时),因此它会转义斜杠。 我有这样的路线: Rails.application.routes.draw do scope “(:locale)”, locale: /#{I18n.available_locales.join(“|”)}/ do resources :machines, except: :destroy do collection do get :search get ‘search/:ad_type(/:machine_type(/:machine_subtype(/:brand)))’, action: ‘search’, as: :pretty_search get ‘:subcategory/:brand(/:model)/:id’, action: ‘show’, as: :pretty patch ‘:subcategory/:brand(/:model)/:id’, action: ‘update’ # To be able to update machines with […]

Rails 4 MySQL bigInt主键问题和错误

我需要在rails 4.1.8应用程序中使用14位bigInt作为主键。 使用SO上的旧post作为指导,我想出了以下内容来解决这个问题…… class CreateAcctTransactions false do |t| t.integer :id, :limit => 8,null: false t.integer “account_id”,limit: 8,null: false t.integer “transaction_type_id”, null: false t.datetime “date”,null: false t.text “description”,limit: 255 t.decimal “amount”,precision: 10, scale: 2, null: false end end end 但是,这种方法并没有真正将“id”指定为主键 ,它只是另一个普通字段。 此外,当我收到以下错误时…… Mysql2 ::错误:字段’id’没有默认值:INSERT INTO acct_transactions ( account_id , amount , date , description , transaction_type_id […]