0
您好,我需要在我的厨房库存网站上的Ruby on rails的帮助。我面临的问题是。 我有一个表名Ingredient
,它的属性是Name, Image, Description
。当我将数据保存在我的Ingredient
表中时,我希望在我的索引视图上显示保存的数据,如同我从选项选择中选择选项一样。然后数据会显示。基于选项select(Rails)获取并显示来自数据库的所有值
在我索引视图当我选择选项“苹果”从 选项选择菜单然后将数据与“苹果”显示我的 索引视图简单的话。其他方面它不显示。
这是我索引视图
<%= 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
I try to find the solution on stack overflow but i don't know how i use this for me.