有以下代码:
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'URL must point to GIT/JPG/PNG pictures'
}
end
Run Code Online (Sandbox Code Playgroud)
它有效,但是当我尝试使用"rake test"测试它时,我会收到这条消息:
rake aborted!
The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我该如何解决?
我有一个阵列["Lorem", "", "ipsum"]
.我想从这个数组中删除空字符串并获取["Lorem", "ipsum"]
.
有没有办法在不使用循环并遍历每个元素并删除它的情况下执行此操作?
我试图使用命令预编译资产rake assets:precompile RAILS_ENV=production
,但我总是得到以下错误.
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/bin/ruby /usr/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
LoadError: cannot load such file -- uglifier
(in /home/cool_tech/cool_tech/app/assets/javascripts/application.js)
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `block in require'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:236:in `load_dependency'
/usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.14/lib/active_support/dependencies.rb:251:in `require'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/compressors.rb:31:in `registered_js_compressor'
/usr/lib/ruby/gems/2.0.0/gems/actionpack-3.2.14/lib/sprockets/bootstrap.rb:18:in `block in run' …
Run Code Online (Sandbox Code Playgroud) ruby-on-rails precompile ruby-on-rails-3 asset-pipeline ruby-on-rails-3.2
我在布局中使用以下代码来显示两种类型的flash消息:
<% if !flash[:notice].nil? %>
<div class="row">
<div class="flash notice col-xs-12">
<%= flash[:notice] %>
</div>
</div>
<% end %>
<% if !flash[:error].nil? %>
<div class="row">
<div class="flash error col-xs-12">
<%= flash[:error] %>
</div>
</div>
<% end %>
<%= debug(flash[:notice]) %>
<%= debug(flash[:error]) %>
Run Code Online (Sandbox Code Playgroud)
它们都工作正常,但无论何时触发,它仍然会出现一个额外的页面视图.我没有使用任何缓存宝石.
为什么会这样?我该如何解决?
我想创建一个新文件夹app/assets
,在这个文件夹中我有一些文件,每个文件都有css文件夹和javascript文件夹,如下面给出的地址:
app/assets/plugins/bootstrap/css/bootstrap.min.css
app/assets/plugins/bootstrap/js/bootstrap.min.js
我使用下面的代码来定义css
文件,但不是true,rails无法找到并加载bootstrap.min.css
:
<%= stylesheet_link_tag "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "../plugins/bootstrap/css/bootstrap.min", media: "all", "data-turbolinks-track" => true %>
Run Code Online (Sandbox Code Playgroud)
我怎样才能申报css
,并javascript
从plugin
文件夹?
注意:当我想css
通过此地址在浏览器中查看文件时:
http://localhost:3000/assets/plugins/bootstrap/css/bootstrap.min.css
Run Code Online (Sandbox Code Playgroud)
我得到以下错误:
No route matches [GET] "/assets/plugins/bootstrap/css/bootstrap.min.css"
但是对于存在的css文件assets/stylesheets/
我可以在浏览器中通过输入地址看顶部的css文件.为什么不同的beetwin stylesheets
和plugins
目录?
在config/application_controller.rb
我的Rails应用程序目录中的文件中,我找到了以下代码:
class ApplicationController < ActionController::Base
protect_from_forgery
end
Run Code Online (Sandbox Code Playgroud)
任何人都能告诉我project_from_forgery
它的用法和原因是什么?
我在Ubuntu 12.04LTS操作系统中进行Rails开发.
我想在一个文件中捕获我的本地IP地址,而不是我使用的环回127.0.0.1 ifconfig
.请提出解决方案.
我通过编辑db/seed.rb
文件和执行rake db:seed
命令将一行数据播种到我的表中.在不知不觉中,我把一些错误的信息放到那一行.所以我想删除以前添加的数据行.是否有同样喜欢任何rake命令rake db:rollback
的rake db:migrate
.
我知道如何在已创建的迁移文件中添加默认值.即
`rails generate migration AddTestColumnToTesttable test_status:boolean` to create it.
Run Code Online (Sandbox Code Playgroud)
它将生成此迁移:
class AddTestColumnToTable < ActiveRecord::Migration
def change
add_column :table, :test_status, :boolean, :default => true
end
end
Run Code Online (Sandbox Code Playgroud)
但是,我们可以通过rails g migration
命令本身添加默认值吗?
我正在使用Bootstrap multiselect,我想添加一个选项来取消选中所有选定的选项.请为我提供相同的解决方案.谢谢你的帮助 :)