如何保存使用simple_form复选框检查的数据?

时间:2019-06-07 13:06:58

标签: ruby-on-rails ruby checkbox simple-form

我是Ruby on Rails的初学者,并且我正在尝试构建一个小应用程序,以使人们可以在一天中从炊具点一个馅饼。

当他们使用复选框选择牧师时,我希望所选的牧师在表战斗中另存为pastrie_id,但我遇到了问题:

Validation failed: Pastrie must exist.

更新它正在工作:

<%= f.association :pastrie, as: :check_boxes, label: 'Pastrie' %>

我仍然有一个问题,我的保存参数不好:

"fight"=>{"pastrie_id"=>["", "1"]},

我尝试了很多关于stackoverflow的解决方案,但似乎没有任何效果。

我正在使用Rails 5.2.3

所以,这是我的模式:

ActiveRecord::Schema.define(version: 2019_06_06_094318) do

  create_table "cookers", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "pastrie"
  end

  create_table "events", force: :cascade do |t|
    t.datetime "date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "fights", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "pastrie_id"
    t.integer "event_id"
    t.integer "cooker_id"
    t.index ["cooker_id"], name: "index_fights_on_cooker_id"
    t.index ["event_id"], name: "index_fights_on_event_id"
    t.index ["pastrie_id"], name: "index_fights_on_pastrie_id"
  end

  create_table "pastries", force: :cascade do |t|
    t.string "pastrie_name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

这是我的模特:

class Event < ApplicationRecord
  has_many :pastries, through: :fights
  has_many :cookers, through: :fights
  has_many :fights
end
class Fight < ApplicationRecord
  belongs_to :pastrie
  belongs_to :event
  belongs_to :cooker, optional: true
end
class Pastrie < ApplicationRecord
  has_many :fights
  has_many :events, through: :fights
end

这是我的控制器。我的理解是:为了创建战斗,我需要一个event_id和pastrie_id(cooker_id是可选的)。因此,首先,我创建一个新事件(因此我有一个event_id),然后,我需要将一个pastrie_id(存在于我的种子中)连接到我的战斗中。但这如果我这样做是行不通的:

class FightsController < ApplicationController

  def new
    @fight = Fight.new
    @event = Event.find(params[:event_id])
    @pastries = Pastrie.all
  end

  def create
    @fight = Fight.new(fight_params)
    @event = Event.find(params[:event_id])
    @fight.event = Event.find(params[:event_id])
    @fight.pastrie_id = params[:fight][:pastrie_id]
    @fight.save!
    redirect_to root_path
  end

  def show
    @events = Event.all
  end

  def index
    @fights = Fight.all
    fights_by_event = []
  end

  private

  def fight_params
    params.require(:fight).permit(:event_id, :pastrie_id, :cooker_id)
  end

end

创建“战斗”时的观点:

<div class=margin-bottom>
  <h2 class=text-center> Bonjour Linguini, quelles patisseries veux-tu choisir ?</h2>
</div>
<div class="container margin-bottom">
  <%= simple_form_for [@event, @fight] do |f| %>
    <% @pastries.each do |pastrie| %>
      <%= f.label :pastrie_id do %>
      <%= f.check_box :pastrie_id, as: :boolean, checked_value: true, unchecked_value: false %> <span><%= pastrie.pastrie_name%></span>
      <% end %></br>
    <% end %>
  <%= f.submit "Valider", class: "btn btn-primary" %>
  <% end %>
</div>

如果您需要,这就是我的路线:

Rails.application.routes.draw do

  root to: 'pages#home'
  resources :events do
    resources :fights, only: [:new, :show, :create]
  end

  resources :fights, only: [:index]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

1 个答案:

答案 0 :(得分:0)

在您的表单更改中

<%= f.check_box :pastrie_id, as: :boolean, checked_value: true, unchecked_value: false %> <span><%= pastrie.pastrie_name%></span>

收件人

<%= f.check_box :pastrie_id, as: :boolean, checked_value: pastrie.id, unchecked_value: nil %> <span><%= pastrie.pastrie_name%></span>

在第一个版本中,您提交了{pastrie_id:true}的参数,该参数显然与糕点无关。第二个版本应提交复选框的ID(尽管如果它仅属于1个糕点,则使这些单选按钮更有意义)