【发布时间】:2014-02-04 21:18:19
【问题描述】:
我从 github 关注了这个 repo,rails-multimodel-upload-demo,了解如何使用回形针和 rails 进行多次上传。我目前正在尝试使我的规格通过,但 atm 有四个错误。
Failures:
1) Cars Cars#show page navigation is the car details nav
Failure/Error: before { visit car_path(car) }
ActionView::Template::Error:
undefined method `upload' for nil:NilClass
# ./app/views/cars/show.html.haml:77:in `block in _app_views_cars_show_html_haml___4520108654775620781_2201394220'
# ./app/views/cars/show.html.haml:71:in `each'
# ./app/views/cars/show.html.haml:71:in `_app_views_cars_show_html_haml___4520108654775620781_2201394220'
# ./spec/requests/cars_spec.rb:19:in `block (3 levels) in <top (required)>'
2) Cars Cars#show page navigation is the car details nav
Failure/Error: before { visit car_path(car) }
ActionView::Template::Error:
undefined method `upload' for nil:NilClass
# ./app/views/cars/show.html.haml:77:in `block in _app_views_cars_show_html_haml___4520108654775620781_2201394220'
# ./app/views/cars/show.html.haml:71:in `each'
# ./app/views/cars/show.html.haml:71:in `_app_views_cars_show_html_haml___4520108654775620781_2201394220'
# ./spec/requests/cars_spec.rb:19:in `block (3 levels) in <top (required)>'
3) Cars /inventory navigation is the normal main nav
Failure/Error: before { visit inventory_path }
ActionView::Template::Error:
undefined method `upload' for nil:NilClass
# ./app/views/cars/index.html.haml:22:in `block in _app_views_cars_index_html_haml___2242562919032452782_2174180500'
# ./app/views/cars/index.html.haml:16:in `_app_views_cars_index_html_haml___2242562919032452782_2174180500'
# ./spec/requests/cars_spec.rb:5:in `block (3 levels) in <top (required)>'
4) Cars /inventory navigation is the normal main nav
Failure/Error: before { visit inventory_path }
ActionView::Template::Error:
undefined method `upload' for nil:NilClass
# ./app/views/cars/index.html.haml:22:in `block in _app_views_cars_index_html_haml___2242562919032452782_2174180500'
# ./app/views/cars/index.html.haml:16:in `_app_views_cars_index_html_haml___2242562919032452782_2174180500'
# ./spec/requests/cars_spec.rb:5:in `block (3 levels) in <top (required)>'
Finished in 3.58 seconds
132 examples, 4 failures
cars_spec.rb
require 'spec_helper'
describe "Cars" do
describe "/inventory" do
before { visit inventory_path }
subject { page }
describe "navigation" do
describe "is the normal main nav" do
it { should have_selector("#main-nav") }
it { should_not have_selector("#car-nav") }
end
end
end
describe "Cars#show page" do
let(:car) { FactoryGirl.create(:car) }
before { visit car_path(car) }
subject { page }
describe "navigation" do
describe "is the car details nav" do
it { should have_selector("#car-nav") }
it { should_not have_selector("#main-nav") }
end
end
end
end
show.html.haml
%section#enquire
= link_to contact_path(subject: "Enquiry: #{@car.year}\
#{@car.make} #{@car.model}", anchor: 'send-us-a-message\
'), class: 'button' do
Enquire
%br
%span
for more details
%section#view-more
.row
.small-12.small-centered.columns
%h1
View More
%ul.small-block-grid-1.medium-block-grid-3
- @cars.each do |car|
%li
%p
= car.year
= car.make
= car.model
= link_to image_tag(car.uploads.first.upload.url), car
在这里你可以看到car.uploads.first.upload.url,所以我认为这是发生错误的地方。
我尝试关注How Do I Use Factory Girl To Generate A Paperclip Attachment?,但没有成功。然而,模型与问题相似。
错误提到有一个 nil 类,因此工厂没有进行上传。即使按照前面的“问题”的答案,我也没有得到它的工作。
cars_controller.rb
def show
@cars = Car.without_car(@car).shuffle[0...3]
@previous = @car.id == 1 ? Car.last : Car.find( @car\
.id - 1 )
@next = @car == Car.last ? Car.first : Car.find( @ca\
r.id + 1 )
@uploads = @car.uploads
@bodyid = 'car-details'
end
对我的工厂进行一些更改后
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :car do
make "MyString"
model "MyString"
year 1
seats 1
transmission "MyString"
drive "MyString"
interior "MyString"
exterior "MyString"
uploads { fixture_file_upload(Rails.root.join('spec', 'photos', 'test.jpg'), 'image/jpg') }
end
end
我得到更多错误
Failure/Error: let(:car) { FactoryGirl.create(:car) }
ActiveRecord::AssociationTypeMismatch:
Upload(#2223442800) expected, got String(#2164378420)
通过阅读ActiveRecord::AssociationTypeMismatch,该对象被传递到汽车变量中。 Ruby/Rails/Rspec - ActiveRecord::AssociationTypeMismatch:我不确定这到底是什么关系,但我正在调查。
【问题讨论】:
-
你的@cars 实例在哪里,意味着你从哪里得到它...?
-
如果
car.uploads返回一个空数组,那么.first将变为nil。因此,当不存在上传时,您应该检查您的固定装置或使视图正常工作。此评论基于您的 haml 文件中的最后一行 -
来自 cars_controller 的@RahulSingh。更新了控制器的问题#show
-
@aross 我的想法完全正确。我尝试使用固定装置修复它,但没有成功。 stackoverflow.com/questions/3294824/…
标签: ruby-on-rails ruby rspec paperclip factory-bot