Refactor customer controller and form to enhance error handling and conditional rendering

This commit is contained in:
2026-08-24 20:17:21 -04:00
parent 8ae82f1423
commit 9dc4b79355
2 changed files with 43 additions and 22 deletions
+26 -9
View File
@@ -8,7 +8,6 @@
# #
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# This controller class will handle map management
class CustomersController < ApplicationController class CustomersController < ApplicationController
include AuthHelper include AuthHelper
@@ -26,8 +25,8 @@ class CustomersController < ApplicationController
include SortHelper include SortHelper
helper :timelog helper :timelog
before_action :add_customer, only: :new before_action :add_customer, only: [:new, :create]
before_action :view_customer, except: [:new, :view] before_action :view_customer, except: [:new, :create, :view]
skip_before_action :verify_authenticity_token, :check_if_login_required, only: [:view] skip_before_action :verify_authenticity_token, :check_if_login_required, only: [:view]
def address_to_s(address) def address_to_s(address)
@@ -75,14 +74,28 @@ class CustomersController < ApplicationController
def create def create
@customer = Customer.new(allowed_params) @customer = Customer.new(allowed_params)
@customer.save
log "Customer ##{@customer.id} created successfully." respond_to do |format|
flash[:notice] = t :notice_customer_created if @customer.save
redirect_to @customer log "Customer ##{@customer.id} created successfully."
format.html { redirect_to @customer, notice: l(:notice_successful_create) }
format.json { render json: { id: @customer.id, name: @customer.name }, status: :created }
else
format.html { render :new }
format.json { render json: { errors: @customer.errors.full_messages }, status: :unprocessable_entity }
end
end
rescue => e rescue => e
log "Failed to create customer: #{e.message}" log "Failed to create customer: #{e.message}"
flash[:error] = e.message respond_to do |format|
redirect_to new_customer_path format.html {
flash[:error] = e.message
redirect_to new_customer_path
}
format.json {
render json: { errors: [e.message] }, status: :internal_server_error
}
end
end end
def edit def edit
@@ -136,6 +149,10 @@ class CustomersController < ApplicationController
def new def new
@customer = Customer.new @customer = Customer.new
if request.xhr?
render partial: 'form', layout: false, locals: { hide_submit: true, hide_toolbar: true }
end
end end
def only_one_non_zero?(array) def only_one_non_zero?(array)
+17 -13
View File
@@ -32,23 +32,27 @@
</div> </div>
</div> </div>
<div class="clearfix"> <div class="clearfix">
<%=t(:field_notes)%>: <%= t(:field_notes) %>:
<div class="input"> <div class="input">
<p> <p>
<%= f.text_area :notes, <%= f.text_area :notes,
rows: 8, rows: 8,
class: 'wiki-edit', class: 'wiki-edit',
style: 'width: 95%;', style: 'width: 95%;',
id: 'customer_appointment_description' %> id: 'customer_notes' %>
<%= wikitoolbar_for 'customer_appointment_description' %> <% unless local_assigns[:hide_toolbar] %>
</p> <%= wikitoolbar_for 'customer_notes' %>
<% end %>
</p>
</div> </div>
</div> </div>
<div class="actions"> <% unless local_assigns[:hide_submit] %>
<%= f.submit %> <div class="actions">
</div> <%= f.submit %>
</div>
<% end %>
<% end %> <% end %>
</fieldset> </fieldset>