2015-04-12 26 views
1

我以前在Datamapper的Sinatra應用程序中使用過這種方法,沒有任何問題。 現在它似乎沒有工作。任何想法讚賞。 我的測試:NoMethodError:未定義的方法`first_or_create'for「foo」:字符串

scenario 'add hashtags to posts' do 
visit '/' 
add_post('Stacca', 
     'Hello! out there', 
     %w(foo bar)) 
post = Post.first 
expect(post.hashtag.map(&:text)).to include('foo') 
expect(post.hashtag.map(&:text)).to include('bar') 

我的服務器

post '/posting' do 
username = params['username'] 
message = params['message'] 
hashtag = params['hashtag'].split(' ').map do |hashtag| 
hashtag.first_or_create(text: hashtag) 
end 
Post.create(username: username, message: message, hashtag: hashtag) 
redirect to ('/') 
end 

我的模型:

class Post 
include DataMapper::Resource 
property :id, Serial 
property :username, String 
property :message, String 

has n, :hashtag, through: Resource 
end 

和:

class Hashtag 
include DataMapper::Resource 
has n, :posts, through: Resource 
property :id, Serial 
property :text, String 
end 

謝謝

回答

1
Hashtag.first_or_create(text: hashtag) 

#標籤應該是一個類 即你錯過了資本

1

這條線:

hashtag.first_or_create(text: hashtag) 

應該是:

Hashtag.first_or_create(text: hashtag) # uppercase! 

否則,你只是想打電話給一個不存在的 「first_or_create」 方法對String( 「富」 )你從場景中獲得。 'Hashtag'是你的班級,'hashtag'是你的(String)變量。

+0

謝謝!我只是自己做了一個!但是謝謝... – Stacca

相關問題