搜索功能无法在rails中运行

时间:2016-06-12 19:02:23

标签: ruby-on-rails search

我正在尝试在我的rails应用程序(https://dedesign.herokuapp.com/)中实现搜索表单,并且它没有返回任何内容(甚至搜索查询都没有出现在终端或网址中)。这是我的application.html.erb:

 <!DOCTYPE html>
<html>
<head>
  <title>Danielle's Calligraphy Services</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <%= csrf_meta_tags %>
</head>
<body>
    <% if notice %>
        <p class="alert alert-info" role="alert"><%= notice %></p>
    <% end %>

    <% if alert %>
        <p class="alert alert-warning" role="alert"><%= alert %></p>
    <% end %>

    <nav>
        <div class="navbar navbar-default navbar-fixed-top" id="myNavbar">
            <div class="container-fluid">
            <form class="navbar-form navbar-left" role="search">
                <div class="form-group">
                    <%= form_tag("/products", method: "get", class: "form-inline") do %>
                    <%= text_field_tag(:q, " ", class: "form-control") %>
                    <%= submit_tag("Search", class: "btn btn-default") %>
                    <% end %>
                </div>
            </form>

            <ul class="nav navbar-nav navbar-right">
                <li><%= link_to "Home", static_pages_index_path %></li>
                <li><%= link_to "About", static_pages_about_path %></li>
                <li><%= link_to "Contact", static_pages_contact_path %></li>
                <li><%= link_to "Products", products_path %></li>
                <li>
                <% if user_signed_in? %>
                    <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
                <% else %>
                    <%= link_to('Login', new_user_session_path) %>
                <% end %>   
                </li>
            </ul>
        </div> <!-- end navbar collapse -->
        </div>
    </nav>

<%= yield %>

<footer>
    <div class="copyright">
    &copy; 2016 Danielle Bukvic
    </div>
</footer>

</body>
</html>

我的products_controller.rb

    class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  respond_to :json, :html

  # GET /products
  # GET /products.json

def index
    if params[:q]
      search_term = params[:q]
      @products = Product.where("name LIKE ?", "%#{search_term}%")
    else
      @products = Product.all
    end
    respond_with @products
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @comments = @product.comments.order("created_at DESC").paginate(page: params[:page], :per_page => 1)
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :description, :image_url, :colour, :price)
    end
end

以下是我在GitHub中的代码,以防您需要访问其他任何内容:https://github.com/dbukvic/nameofapp

0 个答案:

没有答案