我从github跟踪了这个repo,rails-multimodel-upload-demo,了解如何使用paperclip和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
开始,该对象被传递到car变量中。 Ruby/Rails/Rspec - ActiveRecord::AssociationTypeMismatch:我不确定这究竟是如何相关的,但我正在研究它。
答案 0 :(得分:0)
当我之前失败的规范通过时,我正在处理上一个和下一个链接功能。这是解决它的答案RSpec and Factory for testing next, prev records
upload
未定义的原因是因为let(:user)
实际上没有为:user
写一条记录。一旦我添加let!(:user) { FactoryGirl.create(:user) }
一切正常