rails根据check_box

时间:2017-10-08 01:36:25

标签: ruby-on-rails ruby activerecord simple-form-for

我有一个表格,可以让我创造不同特色的膳食,这顿饭可以有零至几种致敏成分,我希望用户能够选择过敏原的类型,如果它们存在,所以发送给我模拟定义的过敏原数量。

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

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

但是我得到了一个空实例,但我在params

中得到了很好的选择
>>  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"]} 

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

我的表单

<%= 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

0 个答案:

没有答案