Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:23:43+00:00 2026-06-13T12:23:43+00:00

My code current works just fine, but I can tell that it could be

  • 0

My code current works just fine, but I can tell that it could be done better. So I figured I would ask for some input.

For a given expense, the user can submit, or un-submit. This creates a record in the expense_approvals table, and sets the approval_status to 1 or 0.

The complexity comes when someone un-submits an expense, and then later resubmits it, since the record already exists, I had to put in the logic to handle it, and a very ugly view to go with it.

My gut tells me there are things I can do in the model, and/or controller to make this much cleaner.

So my model looks like this:

class Expense < ActiveRecord::Base
  attr_accessible :amount, :expense_date, :description

  has_one :expense_approval
end

and

class ExpenseApproval < ActiveRecord::Base
  attr_accessible :approval_date, :approval_status, :approver_id, :expense_id

  belongs_to :expense, foreign_key: :expense_id

  validates_uniqueness_of :expense_id
end

and the controller

def submit_expense
  @expense_list = Expense.all  
  expense = Expense.find(params[:id])

  if expense.expense_approval
    approval = ExpenseApproval.find(expense.expense_approval.id)
    approval.approval_status = 1
  else
    approval = ExpenseApproval.new(approval_status: "1", expense_id: expense.id)
  end

  if approval.save
    #flash[:success] = "You have added an submission."
    redirect_to expense_approvals_path
  else
    flash[:error] = "Error"    
    redirect_to expense_approvals_path
  end
end

def un_submit_expense
  @expense_list = Expense.all  
  @expense = ExpenseApproval.find(params[:id])
  @expense.approval_status = 0

  if @expense.save
    #flash[:success] = "You have un submitted."
    redirect_to expense_approvals_path
  else
    flash[:error] = "Unsubmit Error"    
    redirect_to expense_approvals_path
  end
end

and view

 <% if expense_item.expense_approval %>
              <%#=expense_item.expense_approval.approval_status%>

              <% if expense_item.expense_approval.approval_status == 1 %>
                        <%= link_to raw('<i class="icon-arrow-down icon-white"> </i>'), 
                              un_submit_expense_path(expense_item.expense_approval[:id]),
                              class: "btn btn-mini btn-warning", 
                              :confirm => "Are you sure you want to un submit this expense?" %>
              <% end %>

              <% if expense_item.expense_approval.approval_status == 0 %>
                        <%= link_to raw('<i class="icon-arrow-up icon-white"> </i>'), 
                              submit_expense_path(expense_item[:id]),
                              class: "btn btn-mini btn-info", 
                              :confirm => "Are you sure you want to submit this expense?" %>
              <% end %>

            <% else %>

             <%= link_to raw('<i class="icon-arrow-up icon-white"> </i>'), 
                              submit_expense_path(expense_item[:id]),
                              class: "btn btn-mini btn-info", 
                              :confirm => "Are you sure you want to submit this expense?" %>

            <% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:23:44+00:00Added an answer on June 13, 2026 at 12:23 pm

    You can clean up the views by using a decorator.

    Additionally, you have some code duplication there, for instance:

    if @expense.save
      flash[:success] = "You have un submitted."
      redirect_to expense_approvals_path
    else
      flash[:error] = "Unsubmit Error"    
      redirect_to expense_approvals_path
    end
    

    Is pretty much the same as:

    if @expense.save
      flash[:success] = "You have un submitted."
    else
      flash[:error] = "Unsubmit Error"    
    end
    redirect_to expense_approvals_path
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GAE app that works just fine from my browser, but can't
Im trying to generate a color gradient using ColdFusion. My current code below works
I have a problem with obtaining the current user location. Code at first works
My current code uploads image successfully but still it does not show Toast message
I think this code should be fine but Static Analyzer doesn't like it. I
I have a feeling that I'm missing something obvious but can't seem to resolve
On my current project, I am using FxCop to work through various code analysis
current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
My current code is this $swift = email::connect(); $swift->setSubject('hello') ->setFrom(array('alex@example.com.au' => 'Alex')) ->setTo(array('alex@example.com.au' =>
current code I've built function to do something over collection of jQuery elements: var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.