Home>

We become indebted to.

Currently, I am developing an Instagram clone app with rails, and I am writing a code to search the posted content (content column) and display the result, but I can not display the search result and it is clogged.

When I debugged with<% binding.pry%>after the 8th line of the _search.html.erb file, @posts contained the search results normally.

Thanks for your cooperation.

Error when running without<% binding.pry%>

_search.html.erb<% binding.pry%>available

<p>Search</p>
<% = form_tag (search_path ,: method =>'get') do%>
<% = text_field_tag: search%>
<% = submit_tag'Search',: content =>nil%>
<% end%>
<% if @ posts.present?%>
<% @ posts.each do | post |%>
<% binding.pry%>
  <% = post.content%>
<% end%>
<% end%>


Log executed with<% binding.pry%>

[1] pry (#<#<Class: 0x00007f9ce619fed0 >>)>@posts
=>[#<Micropost: 0x00007f9ce61b45d8 id: 11, content: "u", user_id: 1, created_at: Sat, 12 Sep 2020 13:57:33 UTC +00: 00, updated_at: Sat, 12 Sep 2020 13:57 : 33 UTC +00: 00>,
 #<Micropost: 0x00007f9ce61b44e8 id: 10, content: "u", user_id: 1, created_at: Sat, 12 Sep 2020 13:21:09 UTC +00: 00, updated_at: Sat, 12 Sep 2020 13:21:09 UTC +00: 00>]

MicropostsController

class MicropostsController</pre>
<p><br />
micropost.rb</p>
<pre><code>class Micropost  {order (created_at :: desc)}
  validates: user_id, presence: true
  validates: content, presence: true, length: {maximum: 140}
  validates: image, content_type: {in:% w [image/jpeg image/gif image/png],
                                      message: "must be a valid image format"},
                      size: {less_than: 5.megabytes,
                                      message: "should be less than 5MB"}
  #Returns a resized image for display
  def display_image
    image.variant (resize_to_limit: [500, 500])
  end
  def self.search (search)
    return Micropost.all unless search
    Micropost.where (['content LIKE?', "% # {Search}%"])
  end
end
  • Answer # 1

    The second line of _micropost_form.html renders _error_messages.html, but it is an error because object is not passed or nil is passed there.

    If you're not sure, put the file listed above Request in the middle of the attached error screen.