Home>

I want to search for photo titles
When you search, everything will be reflected in veiw.
The database stores photo titles in the text column.

File name
= text_field_tag: search, params [: keyword]
I added params [: keyword] to the search results, but all the search results were reflected and had no effect.

I don't think you can read the tweet.rb if statement well.

Please tell me the cause.

File name<tweets.controller.rb>
 def search
    @tweets = Tweet.search (params [: keyword])
 end
file name<tweet.rb>
def self.search (search)
    if search
      Tweet.where ('text LIKE (?)', "% # {Search}%")
    else
      Tweet.all
    end
end
file name<search.html.haml>
.top-page
    .menu
      = form_tag (tweets_searches_path, method: 'get') do
        = text_field_tag: search
        = submit_tag 'Search', name: nil
    [email protected] tweets.each do | tweet |
      .main-photo {style: "background-image: url (# {tweet.image});"}
file<routes.rb>
Rails.application.routes.draw do
  devise_for: users
  root to: "tweets # index"
  namespace: tweets do
    resources: searches, only:: index
  end
  resources: tweets do
    resources: comments, only: [: create,: destroy]
  end
  resources: users, only:: show
end
File name
class TweetsController
  • Answer # 1

    Is thetext_field_tag: searchpart of

    search.html.hamlnottext_field_tag: keyword?

Related articles