【问题标题】:Get and display all values from database based on option select (Rails)根据选项选择(Rails)从数据库中获取并显示所有值
【发布时间】:2017-07-19 08:13:15
【问题描述】:

您好,我在 Ruby on rails 上的 Kitchen 库存网站上需要帮助。我面临的问题是。 我有一个表名Ingredient,它的属性是Name, Image, Description。当我在Ingredient 表中保存数据时,我想在我的索引视图上显示保存的数据,就像我从选项选择中选择选项一样。然后会显示数据。

在我的 索引视图 中,当我从 选项选择菜单然后数据与我的“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

I try to find the solution on stack overflow but i don't know how i use this for me.

【问题讨论】:

    标签: ruby-on-rails ruby database


    【解决方案1】:

    查看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 %>
    

    您需要根据数据库中的字段进行修改

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多