2016-04-26 8 views
1

この問題を理解しています。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 
+0

こんにちはアレン、**あなたは**レールコンソールで何を見ますか?それを質問に追加できますか?パラメータに関する他の警告/エラーがある場合は、貼り付けてください。また、作成/更新(コンソールが送信時に実行していると言う)アクションはどのように見えますか?それが 'create'アクションであれば、' new'アクションコードも投稿してください。 –

+0

申し訳ありません、上のすべてのコントローラコードを置いておく必要があります。私はそれを更新しました。私はそれを "検査"し、提出される唯一のものは "tablet_id、chauffeur_id、vehicle_id"です。 {"tablet_id" => "2"、 "chauffeur_id" => "15"、 "vehicle_id" => "1"} – Allen

+0

編集中か新モードですか? –

答えて

2

クリーンすべてネストされた属性コードです。これはあなたが望むものではありません。このようなので、モデル:今すぐ

class Inventory < ActiveRecord::Base 
    belongs_to :tablet 
    belongs_to :chauffeur 
    belongs_to :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 

、あなたは、インベントリを作成するとき、あなたは他のもののステータスを更新するようにレールcallbackを使用したいもの。これは、あまりにも、あなたのモデルに行く:

class Inventory < ActiveRecord::Base 
    ... 

    after_create :update_status 

    protected 

    def update_status 
     self.tablet.update_attribute(:status, 2) 
     self.chauffeur.update_attribute(:status, 2) 
     self.vehicle.update_attribute(:status, 2) 
    end 
end 

はまた、あなたのコントローラ上のすべてのごfields_forコードと強いパラメータをきれいにすることを忘れないでください...あなたはもう、ネストされたものを必要としません。

+0

私はそれがネストされた属性を渡したのを見ましたが、私は "新しい"タブレット/運転手/車両を作成しようとしているように見えるこのエラーを受け取ります。 7エラーが保存されているから、この在庫を禁止: タブレットエイリアスが お抱え運転手のchaufnumは空白にすることはできません タブレット電子メールは空白にすることはできません空白にすることはできません 車両vehnumは空白にすることはできません タブレットは空白にすることはできません 運転手は空ではない 車両は空白にできません 「ステータス」を取って各モデルで1-> 2に変更しながら、選択したアイテムを「在庫」モデル(テーブル)に配置する必要があります。 (錠剤/運転手/車両)..これが役立つことを願っています。 – Allen

+0

さて、問題は、私が言ったような 'create'アクションであれば、ここではすべて、タブレット、運転手と乗り物で在庫を構築します。さもなければ、あなたはこのアクションを実行すべきではないので何かが間違っています - 'edit' /' update'でなければなりません。 –

+0

在庫管理システムは、チェックインとチェックアウトのようなものです。だからあなたは在庫フォームに行き、どの車にどの運転手が行くのかを選択します。しかし、あなたがこれらのアイテム/人/車をチェックアウトしているときにも、あなたはすでにチェックアウトされているので、次のチェックアウトのためのドロップダウンでそれらが存在しないようにステータスを更新しています。 これは役に立ちますか、それとも今でも同じ問題ですか?在庫を「作成」する必要があると思うが、新しいタブレット/運転手/車両を作成しているわけではありません。このプロセスが間違っていると、私は助けを使うことができます。 私を助けてくれてどうもありがとう。 – Allen