我被困在一些可能令人难以置信的愚蠢的地方,但我找不到我的问题。我在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 %>

应用程序/型号/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
有帮助吗?

解决方案

恐怕URL是一个保留的词。

http://wiki.rubyonrails.org/rails/pages/resverdwords

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top