我是新来的Rails,并试图做到这一点没有脚手架,所以我会真正学会它。我查看了railstutorial.org,但它与我的项目做的不同,所以我在另一个rails项目中生成了脚手架,并且复制了编辑过的代码。Rails 3 - NoMethodError方法'新'在控制器
环境: Ubuntu的清醒, 红宝石1.9.3p125, 轨3.2.1
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
应用程序/视图/梦/ new.html.erb就是:
<%= render 'form' %>
应用程序/视图/梦/ _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
我已经搜索了几个小时了,无法弄清楚这一点。我会很感激任何帮助!
我认为这与我的模型有关。我得到这个错误在轨控制台 '1.9.3p125:001> @dream = Dream.new NoMethodError:未定义的方法 '新' 的梦想:Module' – nnotreall 2012-04-25 00:09:05