この問題を理解しています。Rails - ネストアトリビュートの問題
私は何を達成しようとしていますか?
ドロップダウンの選択項目がインベントリに送信されました。
フォーム内の各項目のステータスを、それぞれのモデルで変更します。
問題
- 私はフォームを送信し、ドロップダウン内の各アイテムのステータスが提出されません。
助けていただければ幸いです。
ありがとうございます!
フォームのコード
<%= form_for(@inventory, html: {class: "form-horizontal"}) do |f| %>
<% if @inventory.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@inventory.errors.count, "error") %> prohibited this inventory from being saved:</h2>
<ul>
<% @inventory.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :tablet_id, "Tablet #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "tablet_id", Tablet.where("status = '1'").all.collect{ |t| [t.alias, t.id] }, {}, { class: "form-control"}) %>
</div>
</div>
<%= f.fields_for :tablet do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<%= f.label :chauffeur_id, "Chauffeur #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "chauffeur_id", Chauffeur.where("status = '1'").all.collect{ |t| [t.chaufNum, t.id] }, {}, { class: "form-control"}) %>
</div>
</div>
<%= f.fields_for :chauffeur do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<%= f.label :vehicle_id, "Vehicle #", class: "col-md-2 control-label" %>
<div class="col-md-4">
<%= select("inventory", "vehicle_id", Vehicle.where("status = '1'").all.collect{ |t| [t.vehNum, t.id] }, {}, { class: "form-control"}) %>
</div>
</div>
<%= f.fields_for :vehicle do |t| %>
<%= t.hidden_field :status, value: 2 %>
<% end %>
<div class="form-group">
<div class="col-md-2 col-md-offset-2">
<%= f.submit class: "btn btn-sm btn-success" %>
</div>
</div>
<% end %>
モデル(インベントリ)
class Inventory < ActiveRecord::Base
belongs_to :tablet
belongs_to :chauffeur
belongs_to :vehicle
accepts_nested_attributes_for :tablet
accepts_nested_attributes_for :chauffeur
accepts_nested_attributes_for :vehicle
has_paper_trail
validates :tablet_id, :chauffeur_id, :vehicle_id, presence: true
validates :tablet_id, :chauffeur_id, :vehicle_id, uniqueness: { message: " is already checked out." }
end
在庫コントローラー
class InventoriesController < ApplicationController
before_action :authenticate_user!
before_filter :set_paper_trail_whodunnit
before_action :set_inventory, only: [:show, :edit, :update, :destroy]
# GET /inventories
# GET /inventories.json
def index
@inventories = Inventory.all.paginate(:page => params[:page], :per_page => 15)
end
# GET /inventories/1
# GET /inventories/1.json
def show
@versions = PaperTrail::Version.order('created_at DESC')
end
# GET /inventories/new
def new
@inventory = Inventory.new
end
# GET /inventories/1/edit
def edit
end
def history
@versions = PaperTrail::Version.order('created_at DESC')
end
# POST /inventories
# POST /inventories.json
def create
# @inventory = Inventory.new(inventory_params)
render plain: params[:inventory].inspect
# respond_to do |format|
# if @inventory.save
# format.html { redirect_to @inventory, notice: 'Inventory was successfully created.' }
# format.json { render :show, status: :created, location: @inventory }
# else
# format.html { render :new }
# format.json { render json: @inventory.errors, status: :unprocessable_entity }
# end
# end
end
# PATCH/PUT /inventories/1
# PATCH/PUT /inventories/1.json
def update
respond_to do |format|
if @inventory.update(inventory_params)
format.html { redirect_to @inventory, notice: 'Inventory was successfully updated.' }
format.json { render :show, status: :ok, location: @inventory }
else
format.html { render :edit }
format.json { render json: @inventory.errors, status: :unprocessable_entity }
end
end
end
# DELETE /inventories/1
# DELETE /inventories/1.json
def destroy
@inventory.destroy
respond_to do |format|
format.html { redirect_to inventories_url, notice: 'Inventory was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_inventory
@inventory = Inventory.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def inventory_params
params.require(:inventory).permit(:tablet_id, :chauffeur_id, :vehicle_id, tablet_attributes: [:status => '2'], chauffeur_attributes: [:status => '2'], vehicle_attributes: [:status => '2'])
end
end
こんにちはアレン、**あなたは**レールコンソールで何を見ますか?それを質問に追加できますか?パラメータに関する他の警告/エラーがある場合は、貼り付けてください。また、作成/更新(コンソールが送信時に実行していると言う)アクションはどのように見えますか?それが 'create'アクションであれば、' new'アクションコードも投稿してください。 –
申し訳ありません、上のすべてのコントローラコードを置いておく必要があります。私はそれを更新しました。私はそれを "検査"し、提出される唯一のものは "tablet_id、chauffeur_id、vehicle_id"です。 {"tablet_id" => "2"、 "chauffeur_id" => "15"、 "vehicle_id" => "1"} – Allen
編集中か新モードですか? –