python 的 getattr 的 ruby​​ 等价物是什么

发布于 2024-07-17 12:11:31 字数 499 浏览 10 评论 0原文

我是 Rails 新手,并尝试进行一些重构(在 app/views/shared 中放置一个列出标题的部分渲染器)渲染器显示日期和标题。 然而,渲染器的不同用户使用不同的日期。 通过重构,我有

title_date = list_titles.created_on

对于渲染器的其他用户,我想要

title_date = list_titles.updated_on

那么我可以使用我传递的字符串(使用 :locals 参数)吗? 我知道在 Python 中我可以做到,

date_wanted = 'created_on'
title_date = getattr(list_titles, date_wanted)

但我不知道如何在 ruby​​ 中做到这一点。 (显然,在 Rails 中,我会从调用部分渲染器的视图中传递 date_wanted 字符串。)

I am new to rails and trying to do a little refactoring (putting a partial renderer that lists titles in app/views/shared ) The renderer shows the dates along with the titles. However different users of the renderer use different dates. Part way through refactoring I have

title_date = list_titles.created_on

For the other user of the renderer I would want

title_date = list_titles.updated_on

So can I use a string I pass through (using the :locals parameter)? I know in Python I could do

date_wanted = 'created_on'
title_date = getattr(list_titles, date_wanted)

but I can't work out how to do that in ruby. (Obviously in rails I would pass the date_wanted string through from the view calling the partial renderer.)

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

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

发布评论

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

评论(4

彩扇题诗 2024-07-24 12:11:31

Ruby 中的等效语句:

date_wanted = :created_on
title_date = list_titles.send(date_wanted)

The equivalent statement in Ruby:

date_wanted = :created_on
title_date = list_titles.send(date_wanted)
再浓的妆也掩不了殇 2024-07-24 12:11:31

我认为原始问题的答案是 send:

irb(main):009:0> t = Time.new
=> Thu Jul 02 11:03:04 -0500 2009
irb(main):010:0> t.send('year')
=> 2009

send 允许您动态调用对象上的任意命名的方法。

I think the answer to the original question is send:

irb(main):009:0> t = Time.new
=> Thu Jul 02 11:03:04 -0500 2009
irb(main):010:0> t.send('year')
=> 2009

send allows you to dynamically call an arbitrarily-named method on the object.

浅紫色的梦幻 2024-07-24 12:11:31

您可以像这样向模型添加一个函数

def get_date(date)
  return created_on if date == 'created'
  return updated_on
end

You could add a function to the model like this

def get_date(date)
  return created_on if date == 'created'
  return updated_on
end
生生漫 2024-07-24 12:11:31

马特的答案对于您的确切问题是正确的,我可能无法理解您的情况,但是......

我会通过本地哈希将整个用户对象传递到部分对象中。

render( 
  :partial => "shared/titles", 
  :object => @titles, 
  :locals => { :user => @user } 
)

然后在部分调用辅助方法中返回每个标题的正确日期,类似于:

<p><%= title_date_for_user(title, user) %></p>

将用户和每个标题对象传递给辅助方法。

def title_date_for_user(title, user)
  case user.date_i_like_to_see
  when "created_on"
    title_date = title.created_on
  when "updated_on"
    title_date = title.updated_on
  end
  return title_date.to_s(:short)
end

date_i_like_to_see 方法驻留在 User 模型中,并根据给定用户特有的某些逻辑返回一个字符串(created_onupdated_on)。

这种方法隐藏了大部分逻辑,让您的视图保持美观和干净。 另外,它使得以后添加特定于给定用户的更多功能变得简单。

Matt's answer is correct for your exact question and I might be way off-mark with understanding your situation but...

I'd pass the entire user object into the partial via the locals hash.

render( 
  :partial => "shared/titles", 
  :object => @titles, 
  :locals => { :user => @user } 
)

Then within the partial call a helper method to return the correct date for each title, something like:

<p><%= title_date_for_user(title, user) %></p>

Pass the user and each title object to the helper method.

def title_date_for_user(title, user)
  case user.date_i_like_to_see
  when "created_on"
    title_date = title.created_on
  when "updated_on"
    title_date = title.updated_on
  end
  return title_date.to_s(:short)
end

The date_i_like_to_see method resides in the User model and returns a string (created_on or updated_on) based on some logic particular to the given user.

This approach tucks away most of the logic and keeps your view nice and clean. Plus it makes it simple to add further features specific to a given user later.

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