【发布时间】:2012-04-19 00:04:02
【问题描述】:
我是 Rails 的新手,我正在尝试不使用脚手架,所以我会真正学习它。我查看了 railstutorial.org,但它与我的项目正在做的不同,所以我在另一个 rails 项目中生成了脚手架并复制编辑过的代码。
环境: Ubuntu 清醒, 红宝石 1.9.3p125, 轨道 3.2.1
转到应用程序的根目录时出现此错误http://localhost:3000/
NoMethodError in DreamController#new
undefined method `new' for Dream:Module
Rails.root: /vagrant/dream
Application Trace | Framework Trace | Full Trace
app/controllers/dream_controller.rb:5:in `new'
这是我的路线.rb
Dream::Application.routes.draw do
resources :dreams do
resources :interpretations
end
root :to => 'dream#new'
end
这是我的控制器:
class DreamController < ApplicationController
def new
@dream = Dream.new
respond_to do |format|
format.html # new.html.erb
end
end
def create
@dream = Dream.new(params[:dream])
respond_to do |format|
if @dream.save
format.html { redirect_to @dream, notice: 'Dream was successfully created.' }
else
format.html { render action: "new" }
end
end
end
end
app/views/dream/new.html.erb 只是:
<%= render 'form' %>
app/views/dream/_form.html.erb:
<%= form_for(@dream) do |f| %>
<% if @dream.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@dream.errors.count, "error") %> prohibited this dream from being saved:</h2>
<ul>
<% @dream.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :text %><br />
<%= f.text_area :text %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我的模型(在 2 个单独的文件中):
class Dream < ActiveRecord::Base
validates :text, :presence => true
has_many :interpretations
end
class Interpretation < ActiveRecord::Base
validates :text, :presence => true
belongs_to :dream
end
我已经用谷歌搜索了几个小时,但无法弄清楚这一点。如有任何帮助,我将不胜感激!
【问题讨论】:
-
我认为这与我的模型有关。我在 Rails 控制台中收到此错误
1.9.3p125 :001 > @dream = Dream.new NoMethodError: undefined method 'new' for Dream:Module
标签: ruby-on-rails ruby-on-rails-3