简体   繁体   English

Rails:在另一个控制器中的模型中的调用方法

[英]Rails: Calling methods in a model in a different controller

Please, I really don't know how best to achieve refactoring this code following the best rails practices. 拜托,我真的不知道如何按照最佳的Rails实践来最佳地重构此代码。 I am trying to call some methods that are defined in a model in my rails application in a different controller. 我试图在其他控制器的Rails应用程序中调用模型中定义的一些方法。

I have a Donations model and a Dashboard Controller and I would like to display some data about donations on the dashboard. 我有一个捐赠模型和一个仪表板控制器 ,我想在仪表板上显示一些有关捐赠的数据。 I want to achieve this by defining the data that I want in some methods and scopes in the Donations model and then call them in the Dashboard controller and then make them available in the *Dashboard views**. 我想通过在Donations模型中的某些方法和范围中定义所需的数据,然后在Dashboard控制器中调用它们,然后使其在* Dashboard视图**中可用,来实现此目的。

But I realized that no data about donation gets displayed on the dashboard, which may be as a result of the fact that the methods in the Donations model doesn't get exposed or are not available to the Dashboard controller when they are called, since they are a different model and different controller. 但是我意识到,关于捐赠的数据没有显示在仪表板上,这可能是由于捐赠模型中的方法未公开或在调用时对于仪表板控制器不可用的事实的结果,因为它们是不同的型号和不同的控制器。

Here is my code 这是我的代码

Donations Model 捐赠模式

class Donation < ApplicationRecord
  belongs_to :program
  scope :paid_count, -> { where(payment: true).count }
  scope :unpaid_count, -> { where(payment: false).count }
  scope :paid_sum, -> { where(payment: true).sum(:amount) }
  scope :deployed_sum, -> { where(deployment: true).sum(:amount) }
  scope :not_deployed_sum, -> { where(deployment: false, payment: true).sum(:amount) }

  def self.deployed_donations_percent
    (deployed_sum.to_f / paid_sum.to_f) * 100
  end

  def self.not_deployed_donations_percent
    (not_deployed_sum.to_f / paid_sum.to_f) * 100
  end
end

Dashboard Controller 仪表板控制器

class DashboardController < ApplicationController
  def index
    # Paid Donations in Chart
    @paid_donations = Donation.paid_count
    # Unpaid Donations in Chart
    @unpaid_donations = Donation.unpaid_count
    # Total Donations Sum
    @total_donations_sum = Donation.paid_sum
    # Deployed Donations
    @deployed_donations = Donation.deployed_sum
    # Not Deployed Donations
    @not_deployed_donations = Donation.not_deployed_sum
    # Deployed Donations Percentage
    @deployed_donations_percent = Donation.deployed_donations_percent
    # Not Deployed Donations Percentage
    @not_deployed_donations_percent = Donation.not_deployed_donations_percent
    @total_donations = Donation.count
    # Paid Donations
    @paid_donations = Donation.paid_count
    # Unpaid Donations
    @unpaid_donations = Donation.unpaid_count

    # All Programs
    @programs = Program.all
  end
end

Dashboard Index 仪表盘索引

<h2>
    DASHBOARD
</h2>

  <h4>Overall Donations</h4>
  <%#= area_chart @donations.map { |pay|{name: pay.payment, data: @donations.where(payment: pay).group_by_day(:created_at).count}}, discrete: true %>
  <%= line_chart Donation.group(:payment).group_by_day(:created_at).count

   <h4>Total Donations</h4>
    <%= number_with_delimiter(@total_donations_sum, :delimiter => ',') %>

    <h4>Deployed</h4>
     <%= number_with_delimiter(@deployed_donations, :delimiter => ',') %>

    <div>
      <%= @deployed_donations_percent.round %>>
    </div>

    <h4>Undeployed</h4>
    <%= number_with_delimiter(@not_deployed_donations, :delimiter => ',') %>

    <div>
      <%= @not_deployed_donations_percent.round %>
    </div>

<h4>Total Donations</h4>
<header>
  <%= @total_donations %>
</header>

<h4>Paid Donations</h4>
<header>
  <%= @paid_donations %>
</header>

<h4>Unpaid Donations</h4>
<header>
  <%= @unpaid_donations %>
</header>

PROGRAMS
<td>Program Name</td>
<td>Sponsored</td>

</tr>
  <% @programs.each do |program| %>
<tr>
    <td><%= program.name %></td>
<td><%= program.donations.count %></td>

Please, I would greatly appreciate some help on how to refactor this code and get the Donations model methods exposed to the Dashboard Controller . 拜托,我将非常感谢您提供一些有关如何重构此代码并将Donations模型方法公开给Dashboard Controller的帮助 Thank you. 谢谢。

You can also try this out. 您也可以尝试一下。

Controllers and models have a folder called concerns where you can keep codes that you want to be shared by different controllers and/or models. 控制器和模型有一个名为担忧的文件夹,您可以在其中保存要由不同控制器和/或模型共享的代码。

Simply move the code that you want to share to the concerns folder, and then call them on each model. 只需将要共享的代码移动到关注文件夹,然后在每个模型上调用它们。

You can define the code in the concerns folder using modules. 您可以使用模块在关注文件夹中定义代码。

Finally, I got it to work. 终于,我开始工作了。

I needed to implement service objects using Plain Old Ruby Objects. 我需要使用Plain Old Ruby Objects实现服务对象。 For each for the actions, I placed them in a separate service, and then called them in the controllers. 对于每个动作,我将它们放置在单独的服务中,然后在控制器中对其进行调用。

The steps are 步骤是

  1. Create a folder under the app directory of the application named services . 在名为services的应用程序的app目录下创建一个文件夹。
  2. Create individual services for each of the controller actions. 为每个控制器操作创建单独的服务。
  3. Call each service that you want on the controller. 在控制器上调用所需的每个服务。

That's all. 就这样。

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

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