我正在关注 Railcasts 上的视频,但我被卡住了。这是我的代码:
locals_controller.rb
def landing
@locals = Local.search(params[:search])
end
local.rb (模型)
def self.search(search)
if search
where('nome LIKE ?', "%#{search}%")
else
all
end
end
和视图landing.html.erb
<% if signed_in? %>
<%= form_tag root_path, :method => :get, :class => "center form-search" do %>
<p>
<%= text_field_tag :search, params[:search], :class => "input-medium search-query" %>
<%= submit_tag "Search", :name => nil, :class => "btn" %>
</p>
<%= render "landing_search" %>
<% end %>
以及引发错误的部分,因为基本上@locals
是 nil ,因为它说"undefined method each for nil:nilClass"
:
_landing_search.html.erb
<% @locals.each do |local| %>
...
我错过了什么?
我还添加了
match '/', to: "locals#landing"
在routes.rb
我的情况下,因为在我的情况下,要显示的页面位于根路径中