我正在为我们的社区构建一个小型分类广告应用程序,人们可以使用带有 params 描述和图像的表单列出待售产品。
这两个参数都在schema.rb
中问题:
我在index.html.erb上嵌入了搜索栏,并创建了一个空白的show.html.erb。目前,搜索栏除了看起来很漂亮外,没有
我很难理解如何将搜索连接起来询问描述参数并将结果发送到我的show.html.erb。
注意:我希望show.html.erb页面及其搜索结果能够美化地镜像我的索引页面并完成此任务,而无需安装任何其他宝石。
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 15)
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render :new
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render :edit
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find_by(id: params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
答案 0 :(得分:0)
将您的索引方法更改为是否有搜索查询。
def index
if params[:query]
@results = Advert.where('lower(description) LIKE ?', '%#{params[:query].downcase}%')
else
@results = Advert.all
end
end
您也可以使用javascript创建过滤器或安装searchkick
gem或者如果您需要一些非常简单的安装rails4-autocomplete