【问题标题】:Simple search form Error Rails 4简单的搜索表单 Error Rails 4
【发布时间】:2024-10-02 22:00:02
【问题描述】:

我正在尝试在我的应用程序的布局中添加一个简单的搜索表单。它应该按标题或描述搜索产品。我在 Products 模型中创建了部分方法,但是当我尝试将 render _search 放入 application.html.erb ,它会使应用程序崩溃,并显示“我们很抱歉,但出了点问题”。任何想法如何解决这个问题? 文件:

app/models/product.rb

class Product < ActiveRecord::Base
#...
def self.search(search)
    search_condition = "%" + search + "%"
    find(:all, :conditions => ['title LIKE ? OR description LIKE ?', search_condition, search_condition])
  end

  #...

app/controllers/products_controller.rb

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
#...
 def search
      @products = Product.search params[:search]
  end
#...

app/views/products/_search.html.erb

<%= form_tag :controller => 'products', :action => 'search', :method => 'get' do %>
      <%= text_field_tag :search, params[:search], :id => 'search_field' %>
      <%= submit_tag "Search", :name => nil %> 
      <%= link_to_function "Clear", "$('search_field').clear()" %>
<% end %>

app/views/products/_search_results.html.erb

    <ul>
     <% @products.each do |product| %>
 <li><%= link_to product.title, :action => 'show', :id => product.id  %></li>
      <% end %>
      </ul>

app/views/layouts/application.html.erb

<!DOCTYPE html> <%# Это файл макета-шаблона для всех веб-страниц%>
<html>



<head>

  <title>MyGift.com</title> 
...
<div id="search">
   <%= render :partial => "products/search" %>
</div>

...

来自服务器控制台的错误: 在布局/应用程序中渲染 store/index.html.erb (9.5ms) 渲染产品/_search.html.erb (0.9ms) 在 499 毫秒内完成 500 个内部服务器错误

【问题讨论】:

  • 在开发模式下运行您的服务器并查看错误日志。或粘贴您的生产日志
  • 您在产品控制器中的搜索操作未呈现/重定向

标签: ruby-on-rails ruby forms ruby-on-rails-4 search


【解决方案1】:

您的代码没有问题。你的代码应该运行得很好。我猜你错过了一些日志信息。最好分享您的日志文件的屏幕截图,以便我们查看是否缺少任何有价值的信息。

【讨论】:

  • 你的意思是文件 log/test.log 吗?或 log/development.log ?
  • development.log 文件或收到此错误时的控制台屏幕截图。
最近更新 更多