FactoryGirl,Paperclip,Rspec undefined上传方法(FactoryGirl, Paperclip, Rspec undefined upload method)

编程入门 行业动态 更新时间:2024-10-25 07:24:53
FactoryGirl,Paperclip,Rspec undefined上传方法(FactoryGirl, Paperclip, Rspec undefined upload method)

我从github上关注如何使用paperclip和rails进行多次上传的repo, rails-multimodel-upload-demo 。 我目前正试图让我的规格通过,但有四个错误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 ,所以我认为这是发生错误的地方。

我试图了解如何使用工厂女孩生成回形针附件? 没有成功。 然而,模型与问题类似。

该错误提到存在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:我不确定这究竟是如何相关的,但我正在研究它。

I followed this repo, rails-multimodel-upload-demo, from github on how to have multiple uploads using paperclip and rails. I am currently trying to make my specs pass, but there are four errors 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

Here you can see car.uploads.first.upload.url, so I think this is where the error is occurring.

I have tried to follow How Do I Use Factory Girl To Generate A Paperclip Attachment? without success. The models are similar to the question however.

The error mentions that there there is a nil class, so the upload wasn't being made in the factory. Even when I followed the previous' questions' answers, I didn't get it to work.

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

After making some changes to my factories

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

I get more errors

Failure/Error: let(:car) { FactoryGirl.create(:car) } ActiveRecord::AssociationTypeMismatch: Upload(#2223442800) expected, got String(#2164378420)

from reading about ActiveRecord::AssociationTypeMismatch, the object is being passed into the car variable. Ruby/Rails/Rspec - ActiveRecord::AssociationTypeMismatch: I'm not sure how this exactly relates, but I'm looking into it.

最满意答案

当我以前的失败规格通过时,我正在处理上一个和下一个链接功能。 这是解决RSpec和Factory用于测试下一个,prev记录的答案

upload未定义的原因是因为let(:user)实际上没有为:user写一条记录。 一旦我添加了let!(:user) { FactoryGirl.create(:user) }一切正常

I was working on a previous and next links feature when my previous failing specs passed. This is the answer that solved it RSpec and Factory for testing next, prev records

The reason upload was not defined was because the let(:user) does not actually write a record for :user. Once I added let!(:user) { FactoryGirl.create(:user) } everything worked

更多推荐

本文发布于:2023-07-20 12:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   方法   Rspec   Paperclip   FactoryGirl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!