Rails - Nest属性问题

时间:2016-04-26 20:10:45

标签: ruby-on-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

1 个答案:

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

现在,您想要使用rails 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代码和强参数 ...您不再需要嵌套代码。