2017-07-19 24 views
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.

回答

0

調查Ransack 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 %> 

您將需要修改這個視域數據庫中的

相關問題