有没有更清洁的方法来做这样的事情?
%w[address city state postal country].map(&:to_sym)
#=> [:address, :city, :state, :postal, :country]
Run Code Online (Sandbox Code Playgroud)
我想我会%s做我想做的事,但事实并非如此.它只需要括号之间的所有内容,并使其中的一个大符号.
只是一个小小的烦恼.
我想在Rails 4中禁用ActiveRecord.我做了以下内容 config/application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all' -- commented
require "action_controller/railtie"
require "action_mailer/railtie"
#require "active_resource/railtie" no need
#require "rails/test_unit/railtie" no need
#require "sprockets/railtie" no need
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module MyApp
class Application < Rails::Application
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个错误
/home/alex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configuration.rb:95:in
method_missing: undefined method active_record for #<Rails::Application::Configuration:0x00000002005c38> (NoMethodError)
Run Code Online (Sandbox Code Playgroud) 我在Rails应用程序中有一个包含数十万条记录的表,它们只有一个created_at时间戳.我正在添加编辑这些记录的功能,所以我想updated_at在表格中添加一个时间戳.在我的迁移中添加列,我想更新所有行以使新updated_at匹配旧created_at,因为这是Rails中新创建的行的默认值.我可以做一个find(:all)并遍历记录,但由于表的大小,这需要几个小时.我真正想做的是:
UPDATE table_name SET updated_at = created_at;
Run Code Online (Sandbox Code Playgroud)
在使用ActiveRecord而不是执行原始SQL的Rails迁移中有更好的方法吗?
我试图在Rails erb模板中显示Carrierwave附件的文件名.以下不起作用:
<%= @page.form.filename %>
Run Code Online (Sandbox Code Playgroud)
这似乎与文档一致.需要一些额外的步骤吗?
我的页面模型如下所示:
class Page < ActiveRecord::Base
mount_uploader :form, FormUploader
end
Run Code Online (Sandbox Code Playgroud)
表单上传器如下所示:
class FormUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(pdf)
end
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试启动我刚刚从GitHub存储库克隆的redux应用程序.
我尝试使用以下命令运行它
npm start
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
> react-redux@1.0.0 start /home/workspace/assignment
> webpack-dev-server --config ./configs/webpack/webpack.config.development.js
sh: 1: webpack-dev-server: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! react-redux@1.0.0 start: `webpack-dev-server --config ./configs/webpack/webpack.config.development.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the react-redux@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, …Run Code Online (Sandbox Code Playgroud) 我在rails中有一个JSON字符串,如下所示:
[{"content":"1D","createdTime":"09-06-2011 00:59"},{"content":"2D","createdtime":"09-06-2011 08:00"}]
Run Code Online (Sandbox Code Playgroud)
哪些是具有属性内容和创建时间的类内容的对象.
我想将此JSON字符串转换为其各自的JSON对象数组,以便我可以运行循环并将JSON解码为其在rails中的对象.我怎样才能做到这一点?
使用gem magick'并使用rails 3.1.0
Fetching gem metadata from http://rubygems.org/.........
Fetching additional metadata from http://rubygems.org/..
Resolving dependencies...
Using rake (10.1.1)
Using multi_json (1.8.4)
Using activesupport (3.1.0)
Using bcrypt-ruby (3.0.1)
Using builder (3.0.4)
Using i18n (0.6.9)
Using activemodel (3.1.0)
Using erubis (2.7.0)
Using rack (1.3.10)
Using rack-cache (1.0.3)
Using rack-mount (0.8.3)
Using rack-test (0.6.2)
Using hike (1.2.3)
Using tilt (1.3.7)
Using sprockets (2.0.4)
Using actionpack (3.1.0)
Using mime-types (1.25.1)
Using polyglot (0.3.4)
Using treetop (1.4.15)
Using mail (2.3.3)
Using actionmailer (3.1.0)
Using arel …Run Code Online (Sandbox Code Playgroud) 我正在为Rails尝试一些示例应用程序.我创建了一些控制器和页面.但是当我尝试访问其中一个时,我在网页上得到一个例外:
Sprockets::FileNotFound in Pages#home
Showing c:/railscode/test_app/app/views/layouts/application.html.erb
where line #6 raised:
couldn't find file 'jquery'
(in c:/railscode/test_app/app/assets/javascripts/application.js:7)
Extracted source (around line #6):
3: <head>
4: <title>TestApp</title>
5: <%= stylesheet_link_tag "application" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %> 8: </head> 9: <body>
Rails.root:`c:/railscode/test_app`
Run Code Online (Sandbox Code Playgroud)
我使用命令创建了一些控制器:
$ rails generate controller Pages home contact
create app/controllers/pages_controller.rb
route get "pages/contact"
route get "pages/home"
invoke erb
create app/views/pages
create app/views/pages/home.html.erb
create app/views/pages/contact.html.erb
invoke rspec
create spec/controllers/pages_controller_spec.rb
create spec/views/pages
create spec/views/pages/home.html.erb_spec.rb
create spec/views/pages/contact.html.erb_spec.rb
invoke helper …Run Code Online (Sandbox Code Playgroud) 运行时rake db:migrate,我收到此错误:
Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Run Code Online (Sandbox Code Playgroud)
我在这里查看了其他人的问题,他们的解决方案都没有帮助我,例如:
解决方案一
mysql.server start
Run Code Online (Sandbox Code Playgroud)
收益:
Starting MySQL
Run Code Online (Sandbox Code Playgroud)
.错误!服务器退出而不更新PID文件(/usr/local/var/mysql/something.pid).
解决方案二
mysqladmin variables | grep socket
Run Code Online (Sandbox Code Playgroud)
收益:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
Run Code Online (Sandbox Code Playgroud)
进一步说明: 我尝试使用自制程序重新安装mysql,这是成功的,我仍然收到相同的错误:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Run Code Online (Sandbox Code Playgroud) 对慢速运行的SQL查询进行自动EXPLAIN.此功能已从Rails 4中删除.
config.active_record.auto_explain_threshold_in_seconds = 0.5
Run Code Online (Sandbox Code Playgroud)
在受控制的情况下,我们发现它很有用.我找不到以下问题的答案.
删除auto-EXPLAIN的基本原理是什么?
(我确定原因是合理的,但我找不到它们是什么.)
有没有办法在我的Rails 4代码库中重新引入auto-EXPLAIN?
(找不到宝石,也没有任何在线信息)
ruby ×4
activerecord ×1
carrierwave ×1
javascript ×1
json ×1
macos ×1
migration ×1
mysql ×1
npm ×1
react-redux ×1
reactjs ×1
redux ×1
sprockets ×1
timestamp ×1