0

我是 Rails 的新手,正在努力理解结构以及方法应该在哪里以及如何实例化。我现在很困惑自己。

从问题来看,这非常有效,这不符合 MCV 规则。

      <%  @showblog.showruns.group(:spec_vote).count.each do |spec,count| %>
      Spec: <%=" #{spec}"%>  Votes:  <%=" #{count}" %> <br/> 
      <% end %>

我的观点应该是 <%= @voted.inspect %> 或类似的。

与其他地方的所有其他东西。在某些情况下,视图上显示的{}只是 nil 或 nil 本身。

我最近的尝试:

    class Voted < ActiveRecord::Base
    def voted(spec,count)
    @showblog = Showblog.find(params[:showblog_id])
    @voted =  @showblog.showruns.group(:spec_vote).count.each do |spec,count|
     " Spec:#{spec} Votes: #{count}"
     end end end

这有一个零。

我已经在不同的地方添加了这个来尝试看看出了什么问题,但即使出现“无方法错误”,我仍然不知道我应该在哪里看到它。
logger.error "--------(some word so I know the file name)-------#{@showblog.inspect}" 我的应用在 heroku.com

class Showblog < ActiveRecord::Base (parent) has_many :showruns, dependent: :destroy end
class Showrun < ActiveRecord::Base (child) belongs_to :showblog end

两个控制器都包含正常的 CRUD 并且工作正常。

4

1 回答 1

0

在模型和控制器中,最好不要在其中添加视图的逻辑。最好的方法是在视图中使用助手。

在你的 app/helpers/application_helper.rb 或其他帮助文件中,添加这个

def vote_spec(blog)
  result = ''
  blog.showruns.group(:spec_vote).count.each do |spec,count|
    result << "Spec: #{spec}  Votes: #{count} <br/>"
    # or the other logic you want to add such as using a table or div
  end
  result.html_safe
end

在你看来:

<%= vote_spec(@showblog) %>
于 2013-08-18T13:46:19.043 回答