我正在尝试在用户当前页面上实现相关文章。我没有使用黑子或类似的东西。我已经尝试过 Uchenna Okafor 的这段代码,但我得到了一个错误。
在我的模型中
#Related Search
def self.related_search(query, join = "AND")
find(:all, :conditions => related_search_conditions(query, join))
end
def self.related_search_conditions(query, join)
query.split(/\s+/).map do |word|
'(' + %w[name instructions].map { |col| "#{col} LIKE #{sanitize('%' + word.to_s + '%')}" }.join(' OR ') + ')'
end.join(" #{join} ")
end
在 show.html.erb 我有
<%= @recipe.related_search %>
我的错误信息是
NoMethodError in Recipes#show
Showing /Users/sigidis/Ruby/food/app/views/recipes/show.html.erb where line #129 raised:
undefined method `related_search' for #<Recipe:0x10d4980a0>
Extracted source (around line #129):
126: <hr />
127:
128:
129: <%= @recipe.related_search %>
130:
131:
132: <hr />
Rails.root: /Users/sigidis/Ruby/food
Application Trace | Framework Trace | Full Trace
app/views/recipes/show.html.erb:129:in `_app_views_recipes_show_html_erb__699416749_2260079280_0'
app/controllers/recipes_controller.rb:82:in `show'
Request
Parameters:
{"id"=>"35"}
Show session dump
Show env dump
Response
Headers:
None
有人可以帮助我吗,我是 Rails 的新手,我将不胜感激。提前致谢。
参考。[http://stackoverflow.com/q/7086092/812668][1]