我曾经有以下
require 'factory_girl'
require_relative '../../support/factories/users.rb'
class UserMailerPreview < ActionMailer::Preview
def invitation
user = FactoryGirl.build(:user)
UserMailer.invitation(user)
end
end
Run Code Online (Sandbox Code Playgroud)
它正在使用FactoryGirl 4.8
现在,我已经更新到FactoryBot 4.8.2
require 'factory_bot'
require_relative '../../support/factories/users.rb'
class UserMailerPreview < ActionMailer::Preview
def invitation
user = FactoryBot.build(:user)
UserMailer.invitation(user)
end
end
Run Code Online (Sandbox Code Playgroud)
但是我明白了No such file to load -- factory_bot.rb。我需要什么?
谢谢您的帮助。
我正在使用布尔玛有一列卡片,无论内容如何,它们都需要具有相同的高度。为了实现这一点,我创建了以下课程
.equal-height
display: flex
flex-direction: column
height: 100%
Run Code Online (Sandbox Code Playgroud)
我的 HTML 看起来像
<div class='columns is-multiline'>
<div class='column is-one-fifth'>
<div class='card equal-height'>
<div class='card-content'>
# CONTENT GOES HERE
</div>
<div class='card-footer'>
# FOOTER GOES HERE
</div>
</div>
</div>
<div class='column is-one-fifth'>
<div class='card equal-height'>
<div class='card-content'>
# CONTENT GOES HERE
</div>
<div class='card-footer'>
# FOOTER GOES HERE
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这会产生类似的东西
现在我试图将card-footer粘在卡片的底部,如下所示。
我尝试过一些事情,flex但它们并没有真正意义。关于我如何做到这一点有什么想法吗?
我在Rails 5.1应用程序中具有以下内容
class Products::LinkableToEvent
ProductTable = Product.arel_table
EventTable = Event.arel_table
def self.call(slug)
Product.all.
where(ProductTable[:id].in(ProductTable.project(ProductTable[:id])
.except(Product.select(:id)
.joins("INNER JOIN events ON (products.id =
ANY(events.product_ids))")
.where(EventTable[:slug].eq(slug))
.unscope(:order))))
end
end
Run Code Online (Sandbox Code Playgroud)
生成下面的警告。
弃用警告:不推荐将ast委托给arel,并将在Rails 6.0中将其删除。(从/em/app/queries/products/linkable_to_event.rb:8的调用中调用)
8号线是 .except(Product.select(:id)
如何删除警告?
我正在开发一个Rails(3.2)应用程序,我需要在应用程序启动时执行一些任务.
由于我想将逻辑保存在一个单独的文件中,我还创建了一个看起来像的lib/helinium.rb(使用虚拟运行方法)
class Helinium
def self.run
puts "running ...."
end
end
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的初始化文件config/initializers/perform_checks.rb
Helinium.run
Run Code Online (Sandbox Code Playgroud)
一切似乎都很好.现在我希望将Helinium类放在一个模块中,这样两个文件分别看起来像
module Elemens
class Helinium
def self.run
puts "running ...."
end
end
end
Run Code Online (Sandbox Code Playgroud)
和
Elemens::Helinium.run
Run Code Online (Sandbox Code Playgroud)
但是当我尝试启动应用程序时,我得到了
未初始化的常量Elemens(NameError)
我在这里错过了什么吗?为什么找不到模块?
感谢,并有一个愉快的一天.
我怎样才能添加这个
use Rack::Auth::Basic do |username, password|
username == 'pippo' && password == 'pluto'
end
Run Code Online (Sandbox Code Playgroud)
对此
class HelloWorld
def call(env)
req = Rack::Request.new(env)
case req.path_info
when /badges/
[200, {"Content-Type" => "text/html"}, ['This is great !!!!']]
when /goodbye/
[500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]]
else
[404, {"Content-Type" => "text/html"}, ["I'm Lost!"]]
end
end
end
run HelloWorld.new
Run Code Online (Sandbox Code Playgroud)
我有这个简单的 Rack 应用程序,我需要添加 Auth::Basic。
谢谢
我有以下关联
class Training < ApplicationRecord
has_many :attendances
has_many :attendees, through: :attendances
end
class Attendance < ApplicationRecord
belongs_to :training
belongs_to :attendee, class_name: 'Employee'
Run Code Online (Sandbox Code Playgroud)
出勤表有attendee_id和training_id。
现在,我如何Attendance使用 FactoryGirl创建一个有效的?
目前,我有以下代码
FactoryGirl.define do
factory :attendance do
training
attendee
end
end
FactoryGirl.define do
factory :employee, aliases: [:attendee] do
sequence(:full_name) { |n| "John Doe#{n}" }
department
end
end
Run Code Online (Sandbox Code Playgroud)
但我得到
NoMethodError:
undefined method `employee=' for #<Attendance:0x007f83b163b8e8>
Run Code Online (Sandbox Code Playgroud)
我也试过
FactoryGirl.define do
factory :attendance do
training
association :attendee, factory: :employee
end
end
Run Code Online (Sandbox Code Playgroud)
同样的结果。
感谢您的帮助(或者在 SO …
我需要将一个API端点添加到Rails(5.0.2)应用程序,所以我创建了一个TransitControllerunder app/controllers/api.
现在,控制器看起来像
module Api
class TransitController < ActionController::Api
def create
respond_to do |format|
format.json { render :status => :ok, :nothing => true }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我更新了我routes的内容如下
namespace :api do
post 'transits', to: 'transits#create'
end
Run Code Online (Sandbox Code Playgroud)
但现在,当我尝试通过卷曲击中端点时,我得到了
ActionController :: RoutingError(未初始化的常量ActionController :: Api)
在Rails 5+中,ActionController::Api默认情况下不可用?
我在这里错过了什么吗?
谢谢
在 Jekyll (2.5.3) 应用程序上,我想注入一些 HTML 来设置样式,但我很难做到这一点。
这是我的代码
...
markdown: kramdown
highlighter: rouge
...
Run Code Online (Sandbox Code Playgroud)
[Google](https://google.com/) <div class="info">new</div>
Run Code Online (Sandbox Code Playgroud)
但是,一旦构建完成,就会产生以下结果
Google <div class="info">new</div>
Run Code Online (Sandbox Code Playgroud)
HTML 不会被解析,而是简单地呈现为文本。
经过一番谷歌搜索后,我尝试过
<div class="info" markdown="1">new</div>
Run Code Online (Sandbox Code Playgroud)
结尾
markdown: kramdown
kramdown:
parse_block_html: true
highlighter: rouge
Run Code Online (Sandbox Code Playgroud)
但结果总是一样的。
我在这里错过了什么吗?
谢谢。
Ruby中是否可以委托另一个类的类方法来替换下面的方法?
def sugar(param)
Klass.a_very_long_method_name(param)
end
Run Code Online (Sandbox Code Playgroud)
我试过用 Forwardable
extend Forwardable
def_delegator 'Klass.a_very_long_method_name', :sugar
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.
我发现自己要Klass.a_very_long_method_name在很多地方打电话,我正在努力让事情干涸.
在 Rails (5.2) 应用程序中,我尝试使用 JBuilder 返回一些 JSON 作为响应。
我在我的Gemfile.
# Gemfile
...
gem 'jbuilder', '~> 2.5'
...
# Gemfile.lock
...
jbuilder (2.8.0)
...
Run Code Online (Sandbox Code Playgroud)
根据 JBuilder 文档:
您也可以直接从数组中提取属性。
@people = People.all
json.array!@people, :id, :name
=> [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
现在,在我的控制器中,我添加了以下内容:
def index
respond_to do |format|
format.json { render json.array! User.all, :email, :full_name }
end
end
Run Code Online (Sandbox Code Playgroud)
但我得到
NameError - UsersController:0x00007fe2f966f150 16:55:40 rails.1 的未定义局部变量或方法`json'
| => 你的意思是?JSON:
我在这里错过了什么吗?