繁体   English   中英

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

[英]How can I save the datas checked with simple_form checkbox?

我是Ruby on Rails的初学者,我正在尝试构建一个小应用程序,允许人们从一个炊具订购一个pastrie,选择一天。

当他们使用复选框选择pastrie时,我希望所选的pastrie在表格斗争中保存为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

在你的形式改变

<%= 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个糕点,那么制作这些单选按钮可能更有意义)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM