如何将逻辑放入视图、模型中的范围或方法中?

发布于 2024-09-04 18:12:00 字数 688 浏览 4 评论 0原文

我的视图中有以下内容:

            <% unless contact_email.statuses.empty?%>
                (<%= contact_email.statuses.find(:last).status%>) 
             <% end %>

contact_email 是特定模型的实例。

我可以做这样的事情吗?

class ContactEmail < ActiveRecord::Base
  attr_accessible :contact_id, :email_id, :status, :subject, :body, :date_created, :date_sent

  def status
    unless contact_email.statuses.empty?
      contact_email.statuses.find(:last).status
    end
  end

end

有更好的方法吗?有没有办法使用 ||如果为空,则默认运算符?

基本上,我希望能够在视图中执行以下操作:

<%= contact_email.status =>

如果有值,则显示它,如果没有,则不显示任何内容。

I have the following in the view:

            <% unless contact_email.statuses.empty?%>
                (<%= contact_email.statuses.find(:last).status%>) 
             <% end %>

contact_email is an instance of a specific model.

Could I do something like this?

class ContactEmail < ActiveRecord::Base
  attr_accessible :contact_id, :email_id, :status, :subject, :body, :date_created, :date_sent

  def status
    unless contact_email.statuses.empty?
      contact_email.statuses.find(:last).status
    end
  end

end

is there a better way to do this? is there a way to use the || operator for a default if empty?

Basically, I would like to be able to do the following in the View:

<%= contact_email.status =>

IF there is a value, then display it, if not, show nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-09-11 18:12:00

我会将其更改

def status
  unless contact_email.statuses.empty?
    contact_email.statuses.find(:last).status
  end
end

def status
  return if statuses.empty?
  statuses.find(:last).status
end

这应该使该方法更清晰且更易于理解。

现在在您看来您可以随意拨打电话

<%= contact_email.status =>

I would change this

def status
  unless contact_email.statuses.empty?
    contact_email.statuses.find(:last).status
  end
end

to

def status
  return if statuses.empty?
  statuses.find(:last).status
end

This should make the method cleaner and much more easy to understand.

Now in your view you can call as you want

<%= contact_email.status =>
初心 2024-09-11 18:12:00

您可以直接使用以下

<%= contact_email.statuses.find(:last).status unless contact_email.statuses.empty? %>

OR

#I change methodname as it looks more meaningful
def last_status 
  (self.statuses.empty?)? "defalut string if you want" : self.statuses.find(:last).status 
end

并在您的视图中调用它,例如

   <%= contact_email.last_status %>

You can directly use following

<%= contact_email.statuses.find(:last).status unless contact_email.statuses.empty? %>

OR

#I change methodname as it looks more meaningful
def last_status 
  (self.statuses.empty?)? "defalut string if you want" : self.statuses.find(:last).status 
end

and call it in your view like

   <%= contact_email.last_status %>
美胚控场 2024-09-11 18:12:00

我不确定你在问什么。通过检查,您在此处发布的代码似乎可以实现您想要的功能,但是您不能运行它并找出答案吗?我们没有您的完整代码库或架构,但您(希望)这样做 :P

<%= x %> 输出 x.to_s 的值,并且nil.to_s 是空字符串。如果有的话,您上面定义的 ContactEmail#status 方法将返回最后一个状态,否则 nil。所以是的,你所写的将达到你想要的效果。

如果您想提供默认状态(如果没有),可以(在模型中):

def last_status
  unless contact_email.statuses.empty?
    contact_email.statuses.find(:last).status
  end
end

def status
  last_status || DEFAULT_STATUS
end

DEFAULT_STATUS = "Hello world!"

I'm not sure what you're asking. By inspection, it looks like the code you've posted here will do what you want, but can't you just run it and find out? We don't have your full codebase or schema, but you (hopefully) do :P

<%= x %> outputs the value of x.to_s, and nil.to_s is the empty string. The ContactEmail#status method you defined above returns the last status if there is one, otherwise nil. So yes, what you've written will do what you want.

If you want to provide a default status if there isn't one, how about (in the model):

def last_status
  unless contact_email.statuses.empty?
    contact_email.statuses.find(:last).status
  end
end

def status
  last_status || DEFAULT_STATUS
end

DEFAULT_STATUS = "Hello world!"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文