简体   繁体   中英

How to capture a search term from a form and pass it to a method in Rails app

I am learning rails online and have got as far as creating a simple site with some products in a products table with fields:

id

name

price

description

I have gone to the getbootstrap site and started pinching the code from this example -

https://getbootstrap.com/docs/4.0/examples/jumbotron/

This page has a nice search box in the navbar at the top right. I now have it in my simple site and the container HTML currently looks like this:

<div class="container" >
  <!-- Navigation -->
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <div class="container">
      <a class="navbar-brand" href="#">My Shop</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="/">Products</a>
          </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
          <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
      </div>
    </div>
  </nav>
</div>

My problem is that all of the tutorials I see are creating simple forms using ERB to implement some sort of search functionality. like so:

<% form_tag projects_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

While I want to utilise this nicely rendered HTML form instead.

I don't know how to capture the search term and pass it to a method from my form. The ERB example uses the params hash to store the search term.

What I would like to implement is -

1 - Capture the name of a product entered in the search box

2 - Pass that string to a method in my model that queries the DB for first matching name

3 - gets the record id and uses it to redirect to the Show page with the id to display the item

I have found that setting the form action to "/" causes a click on the submit button to jump to the index method in my controller. From there I am sure I could utilise the model to query the underlying DB but I cannot seem to pass across the search term.

I do have a params hash at that point but it just seems to have a "controller" and an "action" member. Is there an attribute I can set that would add the value to this hash?

Thanks in advance

Brad

You must set an action to the form and a name to the field

<form class="form-inline my-2 my-lg-0" action="/search" method="get">
  <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search", name: "search>
  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

Then define the method in a controller.

StaticPagesController

def search
  @search_term = params[:search]
end

And define your route in routes.rb

match '/search',  to: 'static_pages#search', via: 'get'

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