为什么我的 Rails 实例变量在我看来是 nil ?
我被困在一些可能非常愚蠢的事情上,但我找不到我的问题。我在 ubuntu 机器上使用 Rails 3.0.3 和 ruby 1.9.2。
我试图在我的控制器中设置一个实例变量,并使用它在新视图模板上创建一个表单,但在我的视图中它为零。我知道控制器中设置的实例变量应该复制到视图中,但这里它设置为 nil。谁能看到我做错了什么吗?这是我的错误消息:
NoMethodError in Urls#new
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for @url do |f| %>
2: <%= f.submit %>
3: <% end %>
app/controllers/urls_controller.rb
class UrlsController < ApplicationController
def new
@url = Url.new
end
end
app/views/urls/new.html.erb
<%= form_for @url do |f| %>
<%= f.submit %>
<% end %>
app/models/url.rb
# == Schema Information
# Schema version: 20110122002326
#
# Table name: urls
#
# id :integer not null, primary key
# long_url :text
# short_url :string(255)
# created_at :datetime
# updated_at :datetime
#
class Url < ActiveRecord::Base
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
end
I am stuck on someothing probably incredibly stupid, but I can't find my problem. I'm using Rails 3.0.3, and ruby 1.9.2 on a ubuntu machine.
I am trying to just set an instance variable in my controller, and use it to create a form on a new view template, yet it is coming up nil in my view. I know that instance variables set in the controller are supposed to be copied to the view, but here its set to nil. Can anyone see what I'm doing wrong? Here is my error message:
NoMethodError in Urls#new
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for @url do |f| %>
2: <%= f.submit %>
3: <% end %>
app/controllers/urls_controller.rb
class UrlsController < ApplicationController
def new
@url = Url.new
end
end
app/views/urls/new.html.erb
<%= form_for @url do |f| %>
<%= f.submit %>
<% end %>
app/models/url.rb
# == Schema Information
# Schema version: 20110122002326
#
# Table name: urls
#
# id :integer not null, primary key
# long_url :text
# short_url :string(255)
# created_at :datetime
# updated_at :datetime
#
class Url < ActiveRecord::Base
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕 Url 是一个保留字。
http://wiki.rubyonrails.org/rails/pages/reservedwords
Url is a reserved word I'm afraid.
http://wiki.rubyonrails.org/rails/pages/reservedwords