Ruby on Rails - 模型的前 5 个:形式参数不能是常量
我在尝试将教程模型的前 5 个显示在索引页上时遇到一些问题。 我的教程控制器中的索引方法(用于索引操作)中有以下内容:
@TopTutorials = Tutorial.find(:all, :order => "views DESC", :limit => 5)
然后我使用此实例变量并尝试在索引视图中循环遍历前 5 个:
<% @TopTutorials.each do |TopViewedTutorial| %>
<%= link_to TopViewedTutorial.name, show_tutorial_path(TopViewedTutorial.category_id, TopViewedTutorial.to_param) %>
<% end %>
我对循环遍历事物有点陌生像这样,所以我认为我做错了什么。加载索引页时,我在“.each do”行收到以下错误:
formal argument cannot be a constant
有人能帮我解决这个问题吗?
最好的问候,
乔
I'm having some issues trying to get the top 5 of my tutorial model to display on my index page.
I've got the following in the index method (for the index action) in my tutorial controller:
@TopTutorials = Tutorial.find(:all, :order => "views DESC", :limit => 5)
I then use this instance variable and try to loop through the top 5 in my index view:
<% @TopTutorials.each do |TopViewedTutorial| %>
<%= link_to TopViewedTutorial.name, show_tutorial_path(TopViewedTutorial.category_id, TopViewedTutorial.to_param) %>
<% end %>
I'm kind of new to looping through things like this and so I assume I've done something wrong. On loading up the index page I get the following error at the ".each do" line:
formal argument cannot be a constant
Could anyone help me out with this?
Best Regards,
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将 TopViewedTutorial 更改为以小写字母开头的内容,例如 topViewedTutorials。您可以在此处阅读有关 ruby 名称的更多信息 http://rubylearning.com/satishtalim/ruby_names.html 。
You have to change TopViewedTutorial to something starting with lowercase letter like topViewedTutorials. You could read more about ruby names here http://rubylearning.com/satishtalim/ruby_names.html.