开发者

How to use class method output in view?

I have a class method find_all_media in model abc.rb. Model xyz and abc has relationship,

abc :has_many xyzs and

xyz :belongs_to abc

def self.find_all_media(media_name)

  if self.media_name == self.xyz.media_name
    return media_name
 end
end 

I want to call this method in

 ### xyz/index view f开发者_如何学Cile
<% @abc.xyzs.each do |xyz| %>
<tr>
  <td><%=h xyz.media_name %></td>
  <td><%=h xyz.type %></td>
  <td>I want to call method find all_media here ?? </td>

I tried but not working, any suggestions??


From your view code and the state you maintain, looks like you need an instance method and not class method.

def find_all_media(media_name)
   return media_name if self.media_name == self.xyz.media_name
end


I can see a problem in your class method, you have written it as class method and trying to access the instance methods in it i.e. media_name and xyz.media_name. so make it instance method as rc suggested.

<% @abc.xyzs.each do |xyz| %>
<tr>
  <td><%=h xyz.media_name %></td>
  <td><%=h xyz.type %></td>
  <td><% @abc.find_all_media(xyz.media_name) %></td> # calling find_all_media
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜