我之前在使用Datamapper的Sinatra应用程序中使用过这种方法没有任何麻烦。 现在它似乎不起作用。任何想法都赞赏。 我的测试:
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
谢谢
答案 0 :(得分:1)
这一行:
hashtag.first_or_create(text: hashtag)
应该是:
Hashtag.first_or_create(text: hashtag) # uppercase!
否则,你只是试图呼叫一个不存在的" first_or_create"你从场景得到的String(" foo")上的方法。 '#标签'是你的班级,'#标签'是你的(String)变量。
答案 1 :(得分:1)
Hashtag.first_or_create(text: hashtag)
Hashtag应该是一个班级 即你错过了首都