Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails重构:定制方法&;大视野_Ruby On Rails_Ruby On Rails 3_Refactoring - Fatal编程技术网

Ruby on rails Rails重构:定制方法&;大视野

Ruby on rails Rails重构:定制方法&;大视野,ruby-on-rails,ruby-on-rails-3,refactoring,Ruby On Rails,Ruby On Rails 3,Refactoring,我目前的代码运行得很好,但我可以说它可以做得更好。所以我想我应该征求一些意见 对于给定的费用,用户可以提交或取消提交。这将在费用审批表中创建一条记录,并将审批状态设置为1或0 复杂的是,当有人取消提交费用,然后再重新提交时,因为记录已经存在,我必须输入逻辑来处理它,而且这是一个非常丑陋的观点 我的直觉告诉我,我可以在模型和/或控制器中做一些事情,使其更干净 因此,我的模型如下所示: class Expense < ActiveRecord::Base attr_accessible :

我目前的代码运行得很好,但我可以说它可以做得更好。所以我想我应该征求一些意见

对于给定的费用,用户可以提交或取消提交。这将在费用审批表中创建一条记录,并将审批状态设置为1或0

复杂的是,当有人取消提交费用,然后再重新提交时,因为记录已经存在,我必须输入逻辑来处理它,而且这是一个非常丑陋的观点

我的直觉告诉我,我可以在模型和/或控制器中做一些事情,使其更干净

因此,我的模型如下所示:

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

  has_one :expense_approval
end
和视图

 <% 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 %>

“您确定要取消提交此费用吗?”%%>
“您确定要提交此费用吗?”%%>
“您确定要提交此费用吗?”%%>

您可以使用

此外,还有一些代码重复,例如:

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
与以下内容基本相同:

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

非常感谢。有没有办法清理控制器中我必须检查现有记录的地方?您检索批准的代码应该在模型中。vise,我该怎么做?
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
if @expense.save
  flash[:success] = "You have un submitted."
else
  flash[:error] = "Unsubmit Error"    
end
redirect_to expense_approvals_path