解释Rails中的NoSuchMethod错误

时间:2013-04-14 17:53:14

标签: ruby-on-rails ruby

点击链接时,我的rails应用程序出现以下错误

<%= link_to 'Add New Slinky', new_admin_slinky_path %>

这是错误:

NoMethodError in Admin/slinkys#new
Showing /Users/Me/Folder/Folder/app/views/admin/slinkys/new.html.erb where line #7 raised:

undefined method `model_name' for NilClass:Class
Extracted source (around line #7):

4: <h5>Here is a header</h5>
5: <li>Here is a bullet</li>
6: <br>
7: <%= simple_form_for @slinky_file do |f| %>
8: <%= f.file_field :file %>
9: <br>
10: <%= f.submit "Upload" %>

这个错误是什么意思? 7号线有什么问题?

1 个答案:

答案 0 :(得分:0)

这意味着@slinky_file == nil,你必须在使用前设置它

UPD:

在控制器中

@slinky_file = SlinkyFile.find(params[:slinky_file])

或者,如果要初始化新模型实例

@slinky_file = SlinkyFile.new