您好我需要在Ruby on rails上的Kitchen库存网站上获得帮助。我面临的问题是。
我有一个表名Ingredient
,其属性为Name, Image, Description
。当我在Ingredient
表格中保存数据时,我想在我的索引视图上显示已保存的数据,当我从选项选项中选择选项时,我喜欢。然后会显示数据。
在索引视图上的简单字词中,当我从中选择选项“APPLE”时 选项菜单然后数据与我的“APPLE”显示有关 索引视图。其他方面它没有显示。
这是我的索引视图
<%= select_tag 'choose ingredient', options_from_collection_for_select(current_user.ingredients.all, 'user_id', 'Name'), id: "choose ingredient" %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @ingredients.each do |ingredient| %>
<tr>
<td><%= ingredient.Name %></td>
<td><%= ingredient.Quantity %></td>
<td><%= link_to 'Show', ingredient %></td>
<td><%= link_to 'Edit', edit_ingredient_path(ingredient) %></td>
<td><%= link_to 'Destroy', ingredient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
这是我的控制器
class IngredientsController < ApplicationController
before_action :set_ingredient, only: [:show, :edit, :update, :destroy]
# GET /ingredients
# GET /ingredients.json
def index
@ingredients = Ingredient.where(user_id: current_user)
end
# GET /ingredients/1
# GET /ingredients/1.json
def show
end
# GET /ingredients/new
def new
@ingredient = current_user.ingredients.build
end
# GET /ingredients/1/edit
def edit
end
# POST /ingredients
# POST /ingredients.json
def create
@ingredient = current_user.ingredients.build(ingredient_params)
respond_to do |format|
if @ingredient.save
format.html { redirect_to @ingredient, notice: 'Ingredient was successfully created.' }
format.json { render :show, status: :created, location: @ingredient }
else
format.html { render :new }
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ingredients/1
# PATCH/PUT /ingredients/1.json
def update
respond_to do |format|
if @ingredient.update(ingredient_params)
format.html { redirect_to @ingredient, notice: 'Ingredient was successfully updated.' }
format.json { render :show, status: :ok, location: @ingredient }
else
format.html { render :edit }
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ingredients/1
# DELETE /ingredients/1.json
def destroy
@ingredient.destroy
respond_to do |format|
format.html { redirect_to ingredients_url, notice: 'Ingredient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ingredient
@ingredient = Ingredient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ingredient_params
params.require(:ingredient).permit(:Name, :Quantity)
end
end
答案 0 :(得分:0)
查看Ransack gem。
以下是一个例子:
在索引控制器中安装gem后,设置如下:
def index
ingredients = Ingredient.where(user_id: current_user)
@q = ingredients.ransack(params[:q])
@ingredients = @q.result(distinct: true)
end
现在,您可以在自己的视图中设置搜索字段或下拉列表,根据需要进行过滤:
<%= search_form_for @q do |f| %>
# Search if the name field contains...
<%= f.label :Name_cont %>
<%= f.search_field :Name_cont %>
<%= f.submit %>
<% end %>
您需要根据数据库中的字段修改此内容