【问题标题】:Where do I put in a custom rails method with paramaters and math?我在哪里放置带有参数和数学的自定义 rails 方法?
【发布时间】:2017-01-04 02:08:09
【问题描述】:

我有一个用 ruby​​ 制作的自定义方法,我希望将其放入我的 rails 应用程序中。

您可以在这里进行测试:https://repl.it/Ezqr/20

def time_clock(hour, min)
...
end

我还有一个带有简单脚手架的 rails 应用程序。我跑的脚手架是

rails g scaffold Timeclock hour_in:integer minutes_in:integer

我希望能够将我的自定义 time_clock 方法放入我的 rails 应用程序并在视图中查看它。但是方法去哪儿了?或者至少我应该如何将它实现到我的 Rails 应用程序中?它是进入控制器还是模型?...帮助!

【问题讨论】:

  • 您是否打算只在视图中使用time_clock?它的去向取决于你想在哪里使用它。
  • 好吧,我想显示方法中的返回值。该方法向上或向下舍入数字。这是我的工作用来在人们打卡时整小时的时钟。我想在视图中显示一个新值。
  • 您是否尝试过将其放入帮助程序并尝试访问您视图中的方法?

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


【解决方案1】:

你可以在xxx.html.erb查看这段代码

<div id="page_wrapper">
<p id="notice"><%= notice %></p>

<h1>Listing Timeclocks</h1>

  <% @timeclocks.each do |timeclock| %>
    <div class="timeclock_wrapper">
      <p class="date">
        Submitted <%= time_ago_in_words(timeclock.created_at) %> Ago
      </p>

        <%= minutes, fin_hour_with_30, fin_hour = min_hour_and_half_hour(timeclock) %>
        <p> You are off at <strong><%= "#{fin_hour}:#{minutes}" %></strong> PM Without A lunch </p>
        <p> You are off at <strong><%= "#{fin_hour_with_30}:#{minutes_with_30}" %></strong> PM With A 30 minute lunch </p>
        <p> You are off at <strong><%= "#{fin_hour.to_i + 1}:#{minutes}" %></strong> PM With A 1 Hour lunch</p>
        <p>Inserted Hour: <strong><%= timeclock.hour_in %></strong></p>
        <% if timeclock.minutes_in < 10 %>
        <p>Final Minutes: 0<%= timeclock.minutes_in %></p>
        <p>Inserted Minutes 0<%= timeclock.minutes_in %></p>
        <% else %>
        <p>Final Minutes: <%= timeclock.minutes_in %></p>
        <p>Inserted Minutes <%= timeclock.minutes_in %></p>
        <% end %>
        <button class="button"> <%= link_to 'Show', timeclock %></button>
        <button class="button"> <%= link_to 'Edit', edit_timeclock_path(timeclock) %></button>
        <button class="button"> <%= link_to 'Destroy', timeclock, method: :delete, data: { confirm: 'Are you sure?' } %></button>
    </div>
  <% end %>
<br>

<button class="button"><%= link_to 'New Timeclock', new_timeclock_path %></button>
</div>

你可以创建一个辅助方法,比如helpers/application.rb

module ApplicationHelper

  def min_hour_and_half_hour(timeclock)
    minutes = timeclock.minutes_in % 60
    fin_hour = (timeclock.hour_in + 8) - 12
    if (57 <= minutes) && (minutes <= 59) || (0 <= minutes) && (minutes <= 2)
    minutes = "00"
    minutes_with_30 = "30"
    fin_hour_with_30 =  fin_hour
    elsif (3 <= minutes) && (minutes <= 8  )
      minutes = "06"
      minutes_with_30 = "36"
      fin_hour_with_30 =  fin_hour
    elsif (9 <= minutes) && (minutes <= 14)
      minutes = "12"
      minutes_with_30 = "42"
      fin_hour_with_30 =  fin_hour
    elsif (15 <= minutes) && (minutes <= 20)
      minutes = "18"
      minutes_with_30 = "48"
      fin_hour_with_30 =  fin_hour
    elsif (21 <= minutes) && (minutes <= 26 )
      minutes = "24"
      minutes_with_30 = "54"
      fin_hour_with_30 =  fin_hour
    elsif (27 <= minutes) && (minutes <= 32 )
      minutes = "30"
      fin_hour_with_30 =  fin_hour + 1
      minutes_with_30 = "00"
      fin_hour_with_30 =  fin_hour + 1
    elsif (33 <= minutes) && (minutes <= 38)
      minutes = "36"
      minutes_with_30 = "06"
      fin_hour_with_30 =  fin_hour + 1
    elsif (39 <= minutes) && (minutes <= 44)
      fin_hour_with_30 =  fin_hour + 1
      minutes = "42"
      minutes_with_30 = "12"
    elsif (45 <= minutes) && (minutes <= 50)
      fin_hour_with_30 =  fin_hour + 1
      minutes = "48"
      minutes_with_30 = "18"
    elsif (51 <= minutes) && (minutes <= 56)
     fin_hour_with_30 =  fin_hour + 1
      minutes = "54"  
      minutes_with_30 = "24"
    else
      minutes = min % 60
    end 
    minutes, fin_hour_with_30, fin_hour
  end
end

这将是有点清晰和整洁的代码。

【讨论】:

    【解决方案2】:

    在我看来,我通过使用此代码使其工作。有更好的做法吗?

    <div id="page_wrapper">
    <p id="notice"><%= notice %></p>
    
    <h1>Listing Timeclocks</h1>
    
        <% @timeclocks.each do |timeclock| %>
        <div class="timeclock_wrapper">
            <p class="date">
              Submitted <%= time_ago_in_words(timeclock.created_at) %> Ago
            </p>
                <% minutes = timeclock.minutes_in % 60 %>
                <% fin_hour = (timeclock.hour_in + 8) - 12  %>
                <%if (57 <= minutes) && (minutes <= 59) || (0 <= minutes) && (minutes <= 2) %>
                  <%minutes = "00" %>
                  <%minutes_with_30 = "30" %>
                  <%fin_hour_with_30 =  fin_hour %>
              <%  elsif (3 <= minutes) && (minutes <= 8  ) %>
                <%  minutes = "06" %>
                <%  minutes_with_30 = "36" %>
                <%  fin_hour_with_30 =  fin_hour %>
              <%  elsif (9 <= minutes) && (minutes <= 14) %>
              <%    minutes = "12" %>
              <%    minutes_with_30 = "42" %>
              <%    fin_hour_with_30 =  fin_hour %>
              <%  elsif (15 <= minutes) && (minutes <= 20)   %>
              <%    minutes = "18" %>
              <%    minutes_with_30 = "48" %>
              <%    fin_hour_with_30 =  fin_hour %>
              <%  elsif (21 <= minutes) && (minutes <= 26 )  %>
              <%    minutes = "24" %>
              <%    minutes_with_30 = "54" %>
              <%    fin_hour_with_30 =  fin_hour %>
              <%  elsif (27 <= minutes) && (minutes <= 32 )  %>
              <%    minutes = "30" %>
              <%    fin_hour_with_30 =  fin_hour + 1 %>
              <%    minutes_with_30 = "00" %>
              <%    fin_hour_with_30 =  fin_hour + 1 %>
              <%  elsif (33 <= minutes) && (minutes <= 38)   %>
              <%    minutes = "36"   %>
              <%    minutes_with_30 = "06" %>
              <%    fin_hour_with_30 =  fin_hour + 1 %>
              <%  elsif (39 <= minutes) && (minutes <= 44)   %>
              <%    fin_hour_with_30 =  fin_hour + 1 %>
              <%    minutes = "42" %>
              <%    minutes_with_30 = "12" %>
              <%  elsif (45 <= minutes) && (minutes <= 50)   %>
              <%    fin_hour_with_30 =  fin_hour + 1 %>
              <%    minutes = "48"   %>
              <%    minutes_with_30 = "18" %>
              <%  elsif (51 <= minutes) && (minutes <= 56)   %>
              <%   fin_hour_with_30 =  fin_hour + 1 %>
              <%    minutes = "54"   %>
              <%    minutes_with_30 = "24" %>
              <%  else %>
              <%    minutes = min % 60 %>
              <%  end  %>
    
              <p> You are off at <strong><%= "#{fin_hour}:#{minutes}" %></strong> PM Without A lunch </p>
              <p> You are off at <strong><%= "#{fin_hour_with_30}:#{minutes_with_30}" %></strong> PM With A 30 minute lunch </p>
              <p> You are off at <strong><%= "#{fin_hour.to_i + 1}:#{minutes}" %></strong> PM With A 1 Hour lunch</p>
    
    
    
                <p>Inserted Hour: <strong><%= timeclock.hour_in %></strong></p>
                <% if timeclock.minutes_in < 10 %>
                <p>Final Minutes: 0<%= timeclock.minutes_in %></p>
                <p>Inserted Minutes 0<%= timeclock.minutes_in %></p>
                <% else %>
                <p>Final Minutes: <%= timeclock.minutes_in %></p>
                <p>Inserted Minutes <%= timeclock.minutes_in %></p>
                <% end %>
                <button class="button"> <%= link_to 'Show', timeclock %></button>
                <button class="button"> <%= link_to 'Edit', edit_timeclock_path(timeclock) %></button>
                <button class="button"> <%= link_to 'Destroy', timeclock, method: :delete, data: { confirm: 'Are you sure?' } %></button>
        </div>
        <% end %>
    
    
    <br>
    
    <button class="button"><%= link_to 'New Timeclock', new_timeclock_path %></button>
    </div>
    

    有没有更好的方法来做到这一点?

    【讨论】:

    • 按照 rails_id 的建议将它放在视图助手中会更好。
    • 老实说,你的代码看起来很丑,而且它拥有一切与 Ruby + Rails 相悖的东西
    • 请查看我添加的代码。理想情况下它应该是这样的,你永远不应该在视图端编写那些冗长的方法。欢迎来到 Rails 世界
    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2015-07-25
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2016-02-07
    相关资源
    最近更新 更多