简体   繁体   中英

How can I save the datas checked with simple_form checkbox?

I am beginner with Ruby on Rails, and I am trying to build a little app that permit people to order a pastrie from a cooker, for a day chosen.

When they select the pastrie using a checkbox, I would like that the selected pastrie was saved as pastrie_id in the table fight but instead, I have an issue :

Validation failed: Pastrie must exist.

UPDATE this is working :

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

I still have an issue, the saving params are not good I have :

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

I tried a lot of solutions found on stackoverflow but nothing seems to work.

I am using Rails 5.2.3

So, this is my schema :

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

Here is my models :

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

This is my controller. What I understand is : in order to create a Fight, I need an event_id and a pastrie_id (cooker_id is optional). So first, I create a new event (and so I have an event_id), and next, I need to connect a pastrie_id (existing in my seed) to my fight. But this is not working if I am doing that :

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

And my view when I am creating my "fight" :

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

And this is my routes if you need this :

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

In your form change

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

To

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

In the first version you submit a param of {pastrie_id: true}, which obviously doesn't relate to a Pastry. The second version should submit the ID of the checked box (although if it belongs to only 1 pastry it might make more sense to make these radio buttons)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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