From 6229556e38c5c45cfe054f137865cde45f884649 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 9 Sep 2015 12:58:44 +0200 Subject: [PATCH] Make create in transactions work --- app/controllers/transactions_controller.rb | 12 +++++++----- app/models/client.rb | 1 - app/views/transactions/new.html.haml | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index dbe77c7..74f33a3 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -10,11 +10,13 @@ class TransactionsController < ApplicationController end def create - @transaction = current_user.outgoing_transactions.build( - transaction_params.merge(origin: I18n.t('origin.created_by_user')) + @transaction = Transaction.new(set_params.merge(origin: I18n.t('origin.created_by_user'))) if @transaction.save - redirect_to current_user + respond_to do |format| + format.html { redirect_to root_path } + format.json { head :created } + end else render 'new' end @@ -26,9 +28,9 @@ class TransactionsController < ApplicationController t = params.require(:transaction) .permit(:debtor, :creditor, :amount, :message) - t.update { + t.update({ debtor: User.find_by(name: t[:debtor]) || User.zeus, creditor: User.find_by(name: t[:creditor]) || User.zeus - } + }) end end diff --git a/app/models/client.rb b/app/models/client.rb index bb674c5..cfc2a18 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -13,7 +13,6 @@ class Client < ActiveRecord::Base before_create :generate_key validates :name, presence: true, uniqueness: true - validates :key, presence: true, uniqueness: true def transactions Transaction.where(origin: name) diff --git a/app/views/transactions/new.html.haml b/app/views/transactions/new.html.haml index 68d35f0..35defe4 100644 --- a/app/views/transactions/new.html.haml +++ b/app/views/transactions/new.html.haml @@ -1,6 +1,7 @@ = @transaction.errors.full_messages.join(", ") = 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 :message, required: true = f.submit "Send it!"