我有一个表DIM_STORE
,其中包含列STORE_ID
,STORE_NAME
,CLIENT_CODE
,CREATE_TIMESTAMP
,UPDATE_TIMESTAMP
。
如何在Rails中将新记录保存到oracle db? STORE_ID
,STORE_NAME
和CLIENT_CODE
是varchar
类型的。而且我有一个需要添加新商店的表单。
我收到错误消息,
NoMethodError: undefined method \`returning_id?' for nil:NilClass: INSERT INTO "DIM_STORE" ("STORE_NAME", "STORE_ID", "CLIENT_CODE", "STORE_ID") VALUES (:a1, :a2, :a3, :a4)))
stores_controller.rb,
class StoresController < ApplicationController
protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format == 'application/json'}
before_filter :authenticate_user!, :is_admin
before_action :get_client
before_action :set_store, only: [:show, :edit, :update, :destroy]
# GET /stores
# GET /stores.json
def index
@stores = Store.where("CLIENT_CODE = ?", @client.client_code)
respond_to do |format|
if request.format == :json
format.json { render json: @stores.all}
else
format.js
format.html
end
end
end
# GET /stores/1
# GET /stores/1.json
def show
end
# GET /stores/new
def new
respond_to do |format|
@store = Store.new
format.js
format.html
end
end
def form
end
def new_store
end
# GET /stores/1/edit
def edit
end
# POST /stores
# POST /stores.json
def create
@store = Store.new(store_params)
@store.client_code = @client.client_code
puts @store
respond_to do |format|
if @store.save
format.js
else
format.js {render "stores/new"}
format.json {render json: @store.errors, status: :unprocessable_entity}
end
end
end
#GET /stores/create_success
def create_success
redirect_to request.referrer, notice: 'Store was successfully created under client.'
end
# PATCH/PUT /stores/1
# PATCH/PUT /stores/1.json
def update
respond_to do |format|
if @store.update(store_params)
format.js
else
format.js {render 'stores/edit'}
format.json {render json: @store.errors, status: :unprocessable_entity}
end
end
end
#GET /stores/update_success
def update_success
redirect_to request.referrer, notice: 'Store was successfully updated.'
end
# DELETE /stores/1
# DELETE /stores/1.json
def destroy
stores_users = @store.users
if stores_users.nil? || stores_users.empty?
@store.destroy
redirect_to request.referrer, notice: 'Store was successfully destroyed.'
else
redirect_to request.referrer, notice: 'Users still associated with store. Cannot delete store'
end
end
def get_client
@client = Client.friendly.find(params[:client_id])
puts @client.client_code
=begin
@client = Client.find_by_sql ["SELECT * FROM DIM_CLIENT WHERE client_code = ? ", params[:client_id]]
puts @client;
@client.each do |c|
@client_code = c[:client_code]
@client_name = c[:client_name]
end
puts @client_code
=end
=begin
@client.each do |c|
puts c[:client_code];
end
=end
=begin
@client = Client.find_by_sql ["SELECT client_code FROM DIM_CLIENT WHERE client_code = ? ", :client_code]
=end
end
private
def set_store
=begin
@store = Store.find_by_sql ["SELECT store_id FROM DIM_STORE WHERE store_id = ? ", params[:id]]
=end
@store = Store.friendly.find(params[:id])
end
def store_params
params.require(:store).permit(:store_name, :store_id, :client_code)
end
end