저는 레일을 처음 사용했습니다. 나는 민첩한 웹 개발 서적을 사용하여 레일을 배우려고 노력했지만, 물론 내 자신의 물건을 시험하고 튜토리얼을 따르지 않아야했다. 레이크 테스트를 할 때 나는 이상한 행동을 계속한다.보기에 nil 속성을 to_s로 변환해야합니까?
내가 얻을 :
test_should_show_experiment(ExperimentsControllerTest):
ActionView::Template::Error: can't convert nil into String
및
test_should_get_index(ExperimentsControllerTest):
ActionView::Template::Error: undefined method `name' for nil:NilClass
레이크 테스트를 실행. 내가
<%= image_tag experiment.image_url.to_s, :height => 32, :class => 'list_image' %>
로 (이미지 태그에 .to_s 추가) 모든 기능 테스트가 성공적임을 변경할 때 내 조회수 (인덱스와 쇼)에서
나는
<%= image_tag experiment.image_url, :height => 32, :class => 'list_image' %>
를 사용합니다.
내 질문에 : 레일에 가능한 모든 제로 값을 내 경우에 문자열로 변환해야합니까?
이 문제를 해결하는 데 유용 할 수있는 일부 정보 : 내 모델은 다음과 같습니다 발판 명령을 생성 한 후
class Experiment < ActiveRecord::Base
validates :name, :description, :image_url, :presence => true
validates :name, :uniqueness => true
validates :image_url, :format => {
#:with => %r{\.(gif|jpg|png)$}i,
:with => /\.(gif|jpg|png)$/i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
내가 컨트롤러를 만지지 않았다. 이 곳이다 -
setup do
@experiment = experiments(:one)
@update = {
:name => "JUST SOME UNIT TEST NAME",
:description => "JUST SOME UNIT TEST DESCRIPTION",
:image_url => "http://www.just-some-unit-test-url.example.com/image.jpg"
}
end
나는 ' "실험을 업데이트해야합니다"테스트'를 '테스트 ... "실험을 만들어야합니다"'과의 @update를 사용
class CreateExperiments < ActiveRecord::Migration
def self.up
create_table :experiments do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :experiments
end
end
## another migration file because I wanted to add a column
class AddImageToExperiments < ActiveRecord::Migration
def self.up
add_column :experiments, :image_url, :string
end
def self.down
remove_column :experiments, :image_url
end
end
내 테스트 설정은 다음과 같습니다 오류가 나타납니다.