我已经在rails 3.1上开发了html5-rails和compass-html5但是当我在生产模式下运行我的应用程序并尝试访问我的主页时,我得到以下内容:
Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index
polyfills.js isn't precompiled
Run Code Online (Sandbox Code Playgroud)
问题是localhost:8080:/assets/polyfills.js确实显示.我怀疑我的方法有问题:
重现步骤:
1使用家庭控制器创建新的rails应用程序,索引操作并设置根路由并删除public/index.html和views/layouts/application.html.erb
2在Gemfile中添加以下内容
gem 'rails', '~> 3.1.0'
gem 'unicorn'
group :assets do
gem 'compass', "~> 0.12.alpha.0"
gem 'sass-rails', "~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
gem 'compass-html5', :git => 'https://github.com/sporkd/compass-html5.git'
gem 'html5-rails', :git => "https://github.com/sporkd/html5-rails.git"
end
Run Code Online (Sandbox Code Playgroud)
3运行rails g html5:install以创建/config/compass.rb文件
4创建包含以下代码的/config/initializers/sass.rb:
Rails.configuration.sass.tap do |config|
config.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
end
Run Code Online (Sandbox Code Playgroud)
5运行RAILS_ENV=production bundle exec rake …
我读过 ruby 对象只是我们可以存储实例变量(和类指针)的地方。所以:
class Person
def initialize(age)
@age = age
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果我们运行:
p = Person.new(101)
Run Code Online (Sandbox Code Playgroud)
然后我们得到:
#<Person:0x86cf5b8 @age=101>
Run Code Online (Sandbox Code Playgroud)
太好了,正如预期的那样,属性age存储为实例变量。但是如果我们将模型转换为从 ActiveRecord 继承,事情就会有所不同。现在,在实例化一个新的 Person 之后,我们看到:
# timestamps removed
#<Person id: 1, age: 101 >
Run Code Online (Sandbox Code Playgroud)
该年龄属性似乎不再是一个实例变量。那么这里到底发生了什么?
我知道我们可以访问@attributes实例变量,它包含所有属性和值的散列,所以我想知道 ActiveRecord 是否可能修改控制台输出以这种方式显示对象属性。
是否可以在不使用 ActiveRecord 的情况下实例化一个 Ruby 对象,其中属性作为属性而不是实例变量保存?
我在模型中使用以下代码将链接插入到验证错误消息中:
class Bar < ActiveRecord::Base
has_many :foos
validate :mode_matcher
def mode_matcher
self.foos.each do |f|
errors[:base] << mode_mismatch(foo) unless foo.mode == "http"
end
end
def mode_mismatch(f)
foo_path = Rails.application.routes.url_helpers.foo_path(f)
"Foo <a href='#{foo_path}'>#{f.name}</a> has the wrong mode.".html_safe
end
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我知道推荐的方法是通过区域设置文件。我遇到了麻烦,因为我正在验证另一个模型的属性,所以以下内容不起作用:
en:
activerecord:
errors:
models:
bar:
attributes:
foo:
mode_mismatch: "Foo %{link} has the wrong mode."
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我必须向几百个网站成员发送批量电子邮件.看来有两个体面的Rails ways:
使用简单方法:密送
我们用:bcc同样的方式向所有人发送1封电子邮件:
def send_announcement
users = User.all.pluck(:email)
mail bcc: users, subject: 'yolo'
end
Run Code Online (Sandbox Code Playgroud)
:bcc因为我以前从未这样做过.使用方法很难:到
我们发送468封电子邮件,一次一封,如下:
def send_announcement
User.all.pluck(:email) |email|
mail to: email, subject: 'yolo'
end
end
Run Code Online (Sandbox Code Playgroud)
我有几个问题:
:bcc?:bcc以这种方式使用时会出现什么问题吗?是否可能泄漏/流出用户电子邮件地址?:bccbcc不设置to地址的情况下发送电子邮件?这些电子邮件中唯一的个人身份信息是用户电子邮件地址.是否:bcc总是隐藏呢?我特别询问的原因是,:bcc当我预览电子邮件时,我可以看到收件人的完整列表:localhost:3000/rails/mailers/announcements_mailer/send_announcement.html
假设我已经设置并启用了Sidekiq,并且只需要1或2个小时来实现困难的方式(即使用deliver_later发送多个单独的电子邮件),哪个选项会更好?我不是在考虑可扩展性,因为我们有努力做到这一点.我只想弄清楚简单的方法(bcc方式)是否不赞成?
我喜欢使用简单:bcc技术的单个电子邮件
我喜欢在Rails 5.2中查找用于文件上传的新ActiveStorage模块,但在我为我的网站重写一大堆代码之前,看起来上传仅在用户点击提交按钮时开始.
有没有人知道ActiveStorage是否可以配置为在文件附加后立即上传?
有没有简便的方法可以从Trix工具栏中删除“降低水平”和“升高水平”按钮?
现在,我可以通过使用以下haml简单地渲染自定义工具栏(不包含令人讨厌的按钮)来实现此目的:
%trix-toolbar#trix-toolbar-1
.trix-button-row
%span.trix-button-group.trix-button-group--text-tools{"data-trix-button-group" => "text-tools"}
%button.trix-button.trix-button--icon.trix-button--icon-bold{"data-trix-attribute" => "bold", "data-trix-key" => "b", :tabindex => "-1", :title => "Bold", :type => "button"} Bold
%button.trix-button.trix-button--icon.trix-button--icon-italic{"data-trix-attribute" => "italic", "data-trix-key" => "i", :tabindex => "-1", :title => "Italic", :type => "button"} Italic
%button.trix-button.trix-button--icon.trix-button--icon-link.trix-active{"data-trix-action" => "link", "data-trix-active" => "", "data-trix-attribute" => "href", "data-trix-key" => "k", :tabindex => "-1", :title => "Link", :type => "button"} Link
%span.trix-button-group.trix-button-group--block-tools{"data-trix-button-group" => "block-tools"}
%button.trix-button.trix-button--icon.trix-button--icon-heading-1{"data-trix-attribute" => "heading1", :tabindex => "-1", :title => "Heading", :type => "button"} Heading
%button.trix-button.trix-button--icon.trix-button--icon-quote{"data-trix-attribute" => "quote", …Run Code Online (Sandbox Code Playgroud) 我的网站使用HTML5shiv为Internet Explorer 6用户带来好处.如果禁用JavaScript,会发生什么?
我的花哨新元素(视频,文章等)的CSS是否仍然有效?
有一个非常简单的rails应用程序,这是我的代码:
class Post < ActiveRecord::Base
belongs_to :category
attr_accessible :category_id, :category_attributes
accepts_nested_attributes_for :category, :reject_if => :all_blank
end
class Category < ActiveRecord::Base
has_many :posts
end
#app/views/posts/_form.html.haml
= simple_form_for(@post) do |f|
= f.association :category
= f.simple_fields_for :category do |cat_f|
= cat_f.input :name
Run Code Online (Sandbox Code Playgroud)
因此,在创建新帖子时,我可以选择一个类别(从选择菜单中)或创建一个新类别(如果它不存在).
我想验证category_id是否存在,除非用户选择创建新类别
我猜是有某种类型的rails方法来解决这个问题 - 我知道我不能只是添加,validates :category_id, :presence => true因为这会导致表单提交失败,当用户决定创建一个新类别(并没有从中选择一个落下).
第二个问题: 我最近阅读了一个有用的rails教程,向您展示如何在显示类别选择菜单和新类别字段之间切换,以便在任何给定时间屏幕上只显示两个中的一个.任何人都有这样的链接?
我有以下jquery代码将#contentdiv从另一个页面加载到当前页面的#resultdiv中:
$('a').click(function(event){
$('#result').load('abother_page.html #content');
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将所请求文件的名称以及我想要显示的特定div硬编码到使用load方法发送的字符串中.
我试图让这个动态,但我的代码遗漏了一些东西:
// get the href of the link that was clicked
var href=$(this).attr('href');
// pass the href with the load method
$('#result').load('href #content');
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为我创建的href变量没有插入字符串中.我怎样才能做到这一点?
我尝试了以下,但都没有成功:
// Ruby-style:
$('#result').load('#{href} #content');
// Concatenating-style
$('#result').load(href + '#content');
Run Code Online (Sandbox Code Playgroud) 是否可以访问Sass文件中的Rails.env?
我试着这样做:
$url: <%= Rails.env.production? ? '//foo.com' : '//localhost:3000' %>
@font-face {
src: url("#{$url}/font/fontawesome-webfont.eot");
}
Run Code Online (Sandbox Code Playgroud)
但这给了:
"$ url:"之后的CSS无效:期望的表达式(例如1px,粗体)是"<%= Rails.env.p ..."
我试过不同的文件扩展名,比如css.scss.erb等,但没有快乐.
javascript ×2
actionmailer ×1
activerecord ×1
html ×1
html5 ×1
jquery ×1
rails-i18n ×1
ruby ×1
sass ×1
trix ×1
validation ×1