简体   繁体   English

Rails范围,辅助方法?

[英]Rails Scope, Helper Method?

I have three models. 我有三种模式。 One is an Employee, one is an Item, and one is a Transaction that belongs to both Employee and Items. 一个是雇员,一个是项目,一个是既属于雇员又属于项目的事务。 It's a simple app that allows Employees to check in and check out items - 'Transaction' has a boolean column for checked-in/checked-out. 这是一个简单的应用程序,允许员工签入和签出项目-“交易”具有用于签入/签出的布尔值列。

What I'm trying to do is show within the employee/show view the current list of Items that an Employee has checked out. 我想做的是在员工/显示视图中显示员工已签出的项目的当前列表。 This is some rough code that I sketched out, but I'm not sure that it's going to work, and I was told not to use a lot of nested conditionals in my views anyway. 这是我草绘的一些粗略代码,但是我不确定它是否会起作用,而且我被告知不要在我的视图中使用很多嵌套的条件。

    <% if @employee.transactions.exists? %>
        <h3>Currently Checked-OUT Items</h3>
        <table>
            <tr>
                <th>Item Asset Tag</th>
                <th>Item Description</th>
            </tr>
        <% @employee.transactions.each do |transaction| %>
            <% if item.transaction.last? && transaction.status == false %>
                <tr>
                    <td><% transaction.assettag %></td>
                    <td><% transaction.description %></td>
                </tr>
            <% else %>
            NO CHECKED OUT ITEMS
            <% end %>
        </table>
        <% end %>
    <% end %>   

Basically, I'm trying to: 基本上,我正在尝试:

  • checks all employee transactions 检查所有员工交易
  • compares the item involved in the transaction and sees if it's the .last transaction record for item 比较交易中涉及的物料,并查看其是否为该物料的.last交易记录
  • if it is, and if it's false , then it's a current checkout. 如果为false则为当前结帐。

Is this a better job for a scope within the Transaction model, or a helper method? 对于Transaction模型中的作用域还是辅助方法,这是更好的工作吗? I've never used either, I'm really new at rails. 我从未用过,我真的是新手。

You should do a couple of things in here. 您应该在这里做几件事。

First - create a scope that will fetch last item transaction for you. 首先-创建一个将为您获取最后一笔交易的范围。 There's no point in going through al item transactions if you're interested in the last one only, right? 如果您仅对最后一个感兴趣,则进行项目交易毫无意义,对吧?

Second, use partials. 第二,使用局部。 In this example it's hard to show how I would refactor code to use them (some things doesn't make sense here, ex. where does item variable come from?) 在此示例中,很难显示如何重构代码以使用它们(有些事情在这里没有意义,例如,item变量来自何处?)

Scope example (take last transaction) 范围示例(接受最后一笔交易)

@item.transactions.order('created_at DESC').first

You can as well add scopes for checkin / checkout 您也可以添加签入/签出范围

class Transaction
    scope :checkin, -> { where(status: true) }
    scope :checkout, -> { where(status: false) }
end

First, you are on the right track. 首先,您走在正确的轨道上。 When views get ugly and hard to read because of extensive embedded ruby conditionals and such, think about moving the logic into a helper. 当由于大量嵌入的红宝石条件而使视图变得丑陋且难以阅读时,请考虑将逻辑移入帮助器。

If you have a typical rails app, you'll already have app/helpers/application_helper.rb 如果您有典型的Rails应用程序,则已经有app / helpers / application_helper.rb

So you could just create a helper in that file 所以您可以在该文件中创建一个助手

def make_employee_list(employee)

   if employee.transactions.exists?
     content_tag(:div) do
       content_tag(:h3, "Currently Checked-OUT Items")
       content_tag(:table) do
         employee.transactions.each do |transaction|
           #  you get the idea
         end
       end
     end
   end
end

Then in your view you could do this: 然后,您可以执行以下操作:

<%= make_employee_list(@employee) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM