Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 简单ruby方法助手_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 简单ruby方法助手

Ruby on rails 简单ruby方法助手,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在学习ruby,并想练习为rails编写小助手方法,作为修改基础的好方法。 我只想为作用域对象提供一个计数器 所以在我看来,我写了这个 =stats_counter_for(with_pending_state) “挂起状态”是模型的特定范围 def stats_counter_for(object_state) Photo.object_state.count end 因此,我想通过这个过程来提供具有挂起状态的所有项的计数 所以最终我能做到 =stats_counter_for

我正在学习ruby,并想练习为rails编写小助手方法,作为修改基础的好方法。 我只想为作用域对象提供一个计数器

所以在我看来,我写了这个

=stats_counter_for(with_pending_state)
“挂起状态”是模型的特定范围

def stats_counter_for(object_state)
    Photo.object_state.count
end
因此,我想通过这个过程来提供具有挂起状态的所有项的计数

所以最终我能做到

=stats_counter_for(with_active_state)
=stats_counter_for(with_inactive_state)
(相等值来自haml视图)

更新错误消息

undefined local variable or method `with_pending_state' for #<#<Class:0x007fbce1118230>:0x007fbce1123770>
=link_to '/ Pending Approval', pending_admin_entries_path
=stats_counter_for(with_pending_state)
=link_to '/ Rejected', rejected_admin_entries_path
未定义的局部变量或方法'with_pending_state'#
=链接到“/Pending Approval”,Pending admin条目路径
=stats\u counter\u for(具有挂起状态)
=链接到“/Rejected”,被拒绝的管理条目路径
我哪里出了问题?我相信这是难以置信的简单

您可以使用以下方法:

因此,在您的视图中,您可以这样使用它:

= stats_counter_for(:active) # or as a string 'active'
= stats_counter_for(:inactive)
您可以使用以下方法:

因此,在您的视图中,您可以这样使用它:

= stats_counter_for(:active) # or as a string 'active'
= stats_counter_for(:inactive)

您是在应用程序\ u helper.rb文件中还是在模型中写入此内容?是否可以粘贴确切的错误?如果它在应用程序助手中,则应该working@Edmund谢谢,伙计,这是你在你的应用程序或模型中写的吗?你能粘贴准确的错误吗?如果它在应用程序助手中,则应该working@Edmund谢谢,伙计,这里是你也可以通过批发传递方法名称:
Photo.send(state).count
然后
stats\u counter\u for(:with\u active\u state)
。是的,我知道,但我认为如果在视图中频繁使用,会重复太多。是的,我更喜欢你的方法,但只是提到一个更通用的解决方案。@Stefan有什么可以阻止你编写
=system('rm-rf/')
,对吗?无法抵御程序员。@rico_mac请粘贴照片模型的代码。如果你已经定义了这些作用域,这个助手应该可以工作。你也可以通过批发传递方法名:
Photo.send(state)。count
,然后
stats\u counter\u for(:with\u active\u state)
。是的,我知道,但我认为如果在视图中经常使用,它会太重复。是的,我更喜欢你的方法,但是只提到一个更通用的解决方案。@Stefan有什么可以阻止您编写
=system('rm-rf/')
,对吗?无法抵御程序员。@rico_mac请粘贴照片模型的代码。如果您已经定义了这些作用域,那么这个助手应该可以工作。