Make create in transactions work

This commit is contained in:
benji 2015-09-09 12:58:44 +02:00
parent dbc533a1cb
commit 6229556e38
3 changed files with 9 additions and 7 deletions

View file

@ -10,11 +10,13 @@ class TransactionsController < ApplicationController
end end
def create def create
@transaction = current_user.outgoing_transactions.build( @transaction = Transaction.new(set_params.merge(origin: I18n.t('origin.created_by_user')))
transaction_params.merge(origin: I18n.t('origin.created_by_user'))
if @transaction.save if @transaction.save
redirect_to current_user respond_to do |format|
format.html { redirect_to root_path }
format.json { head :created }
end
else else
render 'new' render 'new'
end end
@ -26,9 +28,9 @@ class TransactionsController < ApplicationController
t = params.require(:transaction) t = params.require(:transaction)
.permit(:debtor, :creditor, :amount, :message) .permit(:debtor, :creditor, :amount, :message)
t.update { t.update({
debtor: User.find_by(name: t[:debtor]) || User.zeus, debtor: User.find_by(name: t[:debtor]) || User.zeus,
creditor: User.find_by(name: t[:creditor]) || User.zeus creditor: User.find_by(name: t[:creditor]) || User.zeus
} })
end end
end end

View file

@ -13,7 +13,6 @@ class Client < ActiveRecord::Base
before_create :generate_key before_create :generate_key
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
validates :key, presence: true, uniqueness: true
def transactions def transactions
Transaction.where(origin: name) Transaction.where(origin: name)

View file

@ -1,6 +1,7 @@
= @transaction.errors.full_messages.join(", ") = @transaction.errors.full_messages.join(", ")
= simple_form_for @transaction do |f| = simple_form_for @transaction do |f|
= f.collection_select :creditor_id, User.all, :id, :name, {}, { class: 'select2-selector' } = f.hidden_field :debtor, value: current_user.name
= f.collection_select :creditor, User.all, :name, :name, {}, { class: 'select2-selector' }
= f.input :amount = f.input :amount
= f.input :message, required: true = f.input :message, required: true
= f.submit "Send it!" = f.submit "Send it!"