简体   繁体   中英

Sending search term from view to service via controller in rails

I am having a hard time understanding search in rails. I want to send search term from my view to service. I have this code in my service:

class Searching
    def search_term
        drive_auth.list_files(q: "fullText contains 'term'",
                              spaces: 'drive',
                              fields: 'nextPageToken, items(id, title)')
    end
end

And this code in controller:

class SearchController < ApplicationController
  def index;  end
  def show
    //here I should take term from search box and send to 
      search_term in service instead of word 'term'
  end
end

And this is my view (I understand it's not full code, that's why I need help)

<p>
  <%= text_field_tag :search, params[:search] %>
  <%= submit_tag "Search", :name => nil %>
</p>

What I want is to search from index.html.erb and show results in show.html.erb page on click. How to send this parametar to search method in service and show results in html?

If you want to send params[:search] to from your controller to service, below code may solve your issue:

In controller:

class SearchController < ApplicationController
  def index;  end
  def show
   result = Searching.new.search_term(params[:search])
   # Your code to use result
  end
end

In service:

class Searching
    def search_term(q)
        drive_auth.list_files(q: "fullText contains '#{q}'",
                              spaces: 'drive',
                              fields: 'nextPageToken, items(id, title)')
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM