在视图中使用创建的 Helper 方法 - rails4
Using a created Helper method in the Views - rails4
我是 rails 的新手,因此非常感谢任何帮助或建议。我试图在我的视图文件中调用名为 "trackable_thing" 的已创建辅助方法,但我收到错误消息:
wrong number of arguments (0 for 1)
[activities/index.html.erb]:我最初在我的视图文件中有:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing = activity.trackable %>
<% if trackable_thing.respond_to? :name %>
<% name = trackable_thing.name %>
<% elsif trackable_thing.respond_to? :content %>
<% name = trackable_thing.content %>
<% end %> </br>
<%= link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>"</br>
<% end %>
[activities_helper.rb]:但我想将视图中的以下逻辑代码转换为辅助方法并将其放在 activities_helper.rb 文件中 - 所以我编写了以下辅助方法
activities/index.html.erb (code changed to a helper method as below)
<% trackable_thing = activity.trackable %>
<% if trackable_thing.respond_to? :name %>
<% name = trackable_thing.name %>
<% elsif trackable_thing.respond_to? :content %>
<% name = trackable_thing.content %>
<% end %>
activities_helper.rb
module ActivitiesHelper
#identifies if the trackable_thing is an event or comment
def trackable_thing
trackable_thing = activity.trackable
if trackable_thing.respond_to? :name
name = trackable_thing.name
elsif trackable_thing.respond_to? :content
name = trackable_thing.content
end
end
[activities/index.html.erb]:我试图在视图中调用辅助方法,但不确定如何调用。我这样称呼它:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing %>
<%= link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>"</br>
<% end %>
error message states: wrong number of arguments (0 for 1)
我也试过如下调用它,但出现错误:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing (link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>")</br>
<% end %>
error message states: syntax error
任何建议或帮助将不胜感激。
是的,ptd 说了什么,trackable_thing 不知道 activity 是什么。使该方法接受一个参数,只要在调用它时传入 activity.
即可将其命名为任意名称
我是 rails 的新手,因此非常感谢任何帮助或建议。我试图在我的视图文件中调用名为 "trackable_thing" 的已创建辅助方法,但我收到错误消息:
wrong number of arguments (0 for 1)
[activities/index.html.erb]:我最初在我的视图文件中有:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing = activity.trackable %>
<% if trackable_thing.respond_to? :name %>
<% name = trackable_thing.name %>
<% elsif trackable_thing.respond_to? :content %>
<% name = trackable_thing.content %>
<% end %> </br>
<%= link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>"</br>
<% end %>
[activities_helper.rb]:但我想将视图中的以下逻辑代码转换为辅助方法并将其放在 activities_helper.rb 文件中 - 所以我编写了以下辅助方法
activities/index.html.erb (code changed to a helper method as below)
<% trackable_thing = activity.trackable %>
<% if trackable_thing.respond_to? :name %>
<% name = trackable_thing.name %>
<% elsif trackable_thing.respond_to? :content %>
<% name = trackable_thing.content %>
<% end %>
activities_helper.rb
module ActivitiesHelper
#identifies if the trackable_thing is an event or comment
def trackable_thing
trackable_thing = activity.trackable
if trackable_thing.respond_to? :name
name = trackable_thing.name
elsif trackable_thing.respond_to? :content
name = trackable_thing.content
end
end
[activities/index.html.erb]:我试图在视图中调用辅助方法,但不确定如何调用。我这样称呼它:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing %>
<%= link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>"</br>
<% end %>
error message states: wrong number of arguments (0 for 1)
我也试过如下调用它,但出现错误:
<h1>Activities</h1>
<% @activities.each do |activity| %>
<% trackable_thing (link_to activity.owner.firstname, activity.owner if activity.owner %> created the event "<%= name %>")</br>
<% end %>
error message states: syntax error
任何建议或帮助将不胜感激。
是的,ptd 说了什么,trackable_thing 不知道 activity 是什么。使该方法接受一个参数,只要在调用它时传入 activity.
即可将其命名为任意名称