0
我正在尝试在用户当前页面上实现相关文章。我不使用太阳黑子或类似的东西。我已经尝试了Uchenna Okafor的这段代码,但是我得到一个错误。相关文章在Ruby on Rails中
在这个模型中,我有
#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]
我仍然得到相同的错误。我是否需要在我的控制器中声明任何内容? – Benjamin
请检查我的编辑,看看是否回答你的问题。 – VNO
我想我明白你的意思。我有一个名为recipe.rb的控制器,这就是上面代码的地方。我试图展示类似于当前食谱的食谱。我曾尝试你的方法,我也得到“引发ArgumentError在食谱#显示 错误的参数数目(0 1) 提取的源(左右线#129): 126:
129:<%= Recipe.related_search% > 132:
应用/视图/食谱/ show.html.erb:129:在'related_search” 应用/视图/食谱/ show.html.erb:129:在'_app_views_recipes_show_html_erb__699416749_2189106480_0' 应用程序/控制器/ recipes_controller.rb:90:在'show' ' – Benjamin