Rails嵌套表单未插入数据库(Oracle)

时间:2019-01-28 10:32:17

标签: ruby-on-rails ruby

我有clients_controllerstores相关联。插入数据时,仅将其插入clients表中,而不会插入stores表中。如何在存储表中插入数据。 client_code是表之间的外键关联。

client.rb

class Client < ActiveRecord::Base
  extend FriendlyId
  has_paper_trail

  self.table_name = "DIM_CLIENT"
  self.primary_key = "client_code"
  self.sequence_name = :autogenerated


  has_many :stores, class_name: 'Store', foreign_key: 'client_code', dependent: :destroy
  accepts_nested_attributes_for :stores, allow_destroy: true, reject_if: :stores_blank
  validates_associated :stores

  validates :client_name, presence: true, uniqueness: {case_sensitive: false}
  validates :client_code, presence: true, uniqueness: {case_sensitive: false}
  after_save :set_default_store

  friendly_id :client_code


end

store.rb

class Store < ActiveRecord::Base
  extend FriendlyId
  has_paper_trail

  self.table_name = "DIM_STORE"
  self.primary_key = "store_id"
  self.sequence_name = :autogenerated

  belongs_to :client, class_name: 'Client', foreign_key: 'client_code'


  validates :store_name, presence: true, uniqueness: { case_sensitive: false }
  validates :store_id, presence: true, uniqueness: { case_sensitive: false }
  validates :client, presence: false

  friendly_id :store_id


end

clients_controller.rb

    class ClientsController < ApplicationController
  protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format == 'application/json'}
  before_filter :authenticate_user!, :is_admin
  before_action :set_client, only: [:show, :edit, :update, :destroy, :set_default_store, :update_default_store]

  # GET /clients/new
  def new
    @client = Client.new
    @store = Store.new
    3.times {@client.stores.build}
  end

  # POST /clients
  # POST /clients.json
  def create
    @client = Client.new(client_params)
    respond_to do |format|
      @client.valid?
      if @client.save
        format.js
      else
        @store = Store.new
        3.times {@client.stores.build}
        format.js {render "clients/new"}
        format.json {render json: @client.errors, status: :unprocessable_entity}
      end
    end
  end

  def create_success
    redirect_to request.referrer, notice: 'Client was successfully created.'
  end

  # PATCH/PUT /clients/1
  # PATCH/PUT /clients/1.json

  def set_default_store
    @stores = @client.stores.all.map {|store| store.code}.sort
  end

  def update_default_store
    if @client.update(update_default_store_params)
      redirect_to request.referrer, notice: 'Default Store successfully updated.'
    else
      redirect_to request.referrer, alert: 'Default Store could not be updated, Please contact support team.'
    end

  end

  private

  def set_client
    @client = Client.friendly.find(params[:id])
  end

  def client_params
    params.require(:client).permit(:client_name, :client_code, :enterprise_code, stores_attributes: [:id, :store_name, :store_id, :_destroy])
  end

  def update_default_store_params
    params.require(:client).permit(:default_store)
  end

end

form.html.erb

    <%= bootstrap_form_for(@client, layout: :group, label_col: "col-sm-2", control_col: "col-sm-4", remote:true) do |f| %>
    <!-- Refer https://github.com/bootstrap-ruby/rails-bootstrap-forms for more on forms-->
    <div style="max-height: 500px; overflow: auto; padding:15px 50px">
      <%= f.text_field :client_name, label: "Client Name", required:true%>
      <%= f.text_field :client_code, label: "Client Code", required:true%>
      <%= f.text_field :enterprise_code, label: "Enterprise Code" %>
      <div class="form-group store" style="padding: 0; margin: 0;">
        <label class="control-label">Stores
          <strong id="storeErrorText" style="color: red">
          <% if @client.errors.full_messages.any? %>
            <% @client.errors.full_messages.each do |error_message| %>
              <%= error_message if @client.errors.full_messages.first == error_message %> <br />
            <% end %>
          <% end %>
          </strong>
        </label>
        <%= f.fields_for :stores, @store do |store| %>
            <%= render 'store_fields', :f => store %>
        <% end %>
      </div>
      <div class="form-group">
        <div class="col-sm-5 col-sm-offset-2" id="btn_col" style="margin-left: 0px; padding-left: 0px">
          <%= link_to_add_association 'Add Store', f, :stores, class: 'btn btn-default btn-xs'%>
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <input class="btn btn--charcoal-ghost" type="button" value="Close"  data-dismiss="modal">
      <%= f.submit :class=> 'btn btn-default', id:'btnClientOps'%>
    </div>
<% end %>

0 个答案:

没有答案