0

我有一个表格,可以让我创建具有不同特征的餐点,餐点可以有零到几种过敏成分,我希望用户能够选择过敏原的类型(如果存在),然后将编号发送给我的模型过敏原的定义。

我可以这样做,@lunch.allergens << Allergen.new但是如何通过许可检索来自参数的数组,我尝试过这种方式。

>>  params.require(:lunch).permit(allergen: :ingredient_name)
=> <ActionController::Parameters {"allergen"=><ActionController::Parameters {} permitted: true>} permitted: true>

但我得到一个空实例,但我在参数中很好地收到了我的选择

>>  params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"******************p07EnQ9JXeadUEPzY/HrX4GaeSm8ajYsP3KiV904VRrjNP54Ni8KL7gF/V2BTHOA==", "lunch"=><ActionController::Parameters {"title"=>"Salade césar et sauté de veau", "description"=>"sdqqsqsc", "portion"=>"3", "price"=>"14", "allergen"=><ActionController::Parameters {"ingredient_name"=>["", "0", "1", "2"]} permitted: false>, "formula"=>{"name"=>"2"}, "supply"=>{"name"=>"2"}, "time_slot"=>{"start_at(1i)"=>"2017", "start_at(2i)"=>"10", "start_at(3i)"=>"8", "start_at(4i)"=>"01", "start_at(5i)"=>"06", "duration"=>"3"}, "room"=>{"name"=>"dehor"}} permitted: false>, "commit"=>"Create Lunch", "controller"=>"lunches", "action"=>"create"} permitted: false>

您可以在此处看到包含我需要的数组的行。

{"ingredient_name"=>["", "0", "1", "2"]} 

问题是成分名称是过敏原表的列名,我知道存在逻辑问题,但是如何在允许我可能需要创建的所有实例的成分名称字段的同时恢复数组。

我的表格

<%= simple_form_for @lunch do |l| %>
  <%= field_set_tag 'Votre menu' do %>
    <%= l.input :title, label: 'Donnez un tire à votre lunch :', error: 'Veuillez specifier' %>
    <%= l.input :description, label: 'Décriver ce qui compose votre lunch :', error: 'Veuillez specifier' %>
    <%= l.input :portion, label: 'Quel nombre de portion aura votre lunch :', error: 'Veuillez specifier' %>
    <%= l.input :price, label: 'Quel sera le prix d une portion :', error: 'Veuillez specifier' %>
    <%= field_set_tag 'Allergenes' do %>
      <%= l.simple_fields_for @allergen do |f| %>
        <%= f.input :ingredient_name, as: :check_boxes, collection: Allergen.allergen_types, label: 'Veuillez selectionner une formule :', error: 'Veuillez specifier' %>
      <% end %>
    <% end %>
    <%= field_set_tag 'Formules' do %>
      <%= l.simple_fields_for @formula do |f| %>
        <%= f.input :name, as: :radio_buttons, collection: Formula.formula_types, label: 'Veuillez selectionner une formule :', error: 'Veuillez specifier' %>
      <% end %>
    <% end %>
  <% end %>
  <%= field_set_tag 'Fournitures' do %>
    <%= l.simple_fields_for @supply do |s| %>
      <%= s.input :name, as: :radio_buttons, collection: Supply.supply_types, label: 'Indiquez si vous fournissez les couverts :', error: 'Veuillez specifier' %>
    <% end %>
  <% end %>
  <%= field_set_tag 'Date et heure' do %>
    <%= l.simple_fields_for @time_slot do |t| %>
      <%= t.input :start_at, as: :datetime, label: 'Date et heure de debut :', error: 'Veuillez specifier' %>
      <%= t.input :duration, label: 'Durée du lunch :', error: 'Veuillez specifier' %>
      <%= field_set_tag 'Lieu' do %>
        <%= l.simple_fields_for @room do |t| %>
          <%= t.input :name, label: 'Lieu du lunch :', error: 'Veuillez specifier' %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>
  <%= l.button :submit %>
<% end %>

我的控制器

class LunchesController < ApplicationController
  before_action :set_lunch, only: [:edit, :show, :update, :destroy]
  before_action :set_dependances, only: [:edit, :show, :update, :destroy]

  def index
    @lunches = Lunch.joins(user: :company).where(users: {company: current_user.company})
  end

  def new
    @lunch = Lunch.new
    @time_slot = TimeSlot.new
    @supplies = Supply.all
    @formulas = Formula.all
    @formula = Formula.new
    @supply = Supply.new
    @room = Room.new
    @allergen = Allergen.new
  end

  def create
    raise
    @formula = Formula.where(formula_params[:formula]).first
    @supply = Supply.where(supply_params[:supply]).first
    @room = Room.new(room_params[:room])
    @room.company = current_user.company
    @room.save!
    @time_slot = TimeSlot.new(time_slot_params[:time_slot])
    @time_slot.room = @room
    @lunch = Lunch.new(lunch_params)
    @lunch.formula = @formula
    @lunch.supply = @supply
    @lunch.time_slot = @time_slot
    @lunch.user = current_user
    if @lunch.save!
      flash[:notice] = "Votre lunch à bien était créer"
      redirect_to lunch_path(@lunch)
    else
      render :new
    end
  end

  def show
  end

  def edit

  end

  def update
    raise
    @formula.update(formula_params[:formula])
    @supply.update(supply_params[:supply])
    @room.update(room_params[:room])
    @time_slot.room = @room
    @time_slot.update(time_slot_params[:time_slot])

    @lunch.formula = @formula
    @lunch.supply = @supply
    @lunch.time_slot = @time_slot
    @lunch.user = current_user
    if @lunch.update!(lunch_params)
      flash[:notice] = "Votre lunch a bien été modifié"
      redirect_to lunches_path
    else
      render :edit
    end
  end

  def destroy
     @lunch.destroy
    redirect_to allergens_path
  end

  private

  def set_lunch
    @lunch = Lunch.find(params[:id])
  end

  def set_dependances
    @supply = @lunch.supply
    @formula = @lunch.formula
    @room = @lunch.room
    @time_slot = @lunch.time_slot
  end

  def lunch_params
    params.require(:lunch).permit(:portion, :title, :description, :price)
  end

  def allergen_params
    params.require(:lunch).permit(allergen: [:ingredient_name])
  end

  def formula_params
    params.require(:lunch).permit(formula: [:name])
  end

  def supply_params
    params.require(:lunch).permit(supply:[:name])
  end

  def time_slot_params
    params.require(:lunch).permit(time_slot: [:start_at, :duration])
  end

  def room_params
    params.require(:lunch).permit(room: [:name])
  end
end
4

0 回答 0