2012-04-19 62 views
1

我是新来的Rails,并试图做到这一点没有脚手架,所以我会真正学会它。我查看了railstutorial.org,但它与我的项目做的不同,所以我在另一个rails项目中生成了脚手架,并且复制了编辑过的代码。Rails 3 - NoMethodError方法'新'在控制器

环境: 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 

应用程序/视图/梦/ 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 

我已经搜索了几个小时了,无法弄清楚这一点。我会很感激任何帮助!

+0

我认为这与我的模型有关。我得到这个错误在轨控制台 '1.9.3p125:001> @dream = Dream.new NoMethodError:未定义的方法 '新' 的梦想:Module' – nnotreall 2012-04-25 00:09:05

回答

0

尝试使控制器名称复数:

root :to => 'dreams#new' 

,并记住更改路由配置文件时重新启动服务器。

+0

谢谢回答!这给了我错误: '未初始化的常量DreamsController' 我将我的DreamController重命名为DreamsController,现在又重新获取原始错误... – nnotreall 2012-04-24 23:50:15

1

我发现了这个问题:我创建了我的导轨项目rails new dream,并且还有一个模型'Dream'。 Rails正在研究应用程序类而不是模型类。当我创建模型时,我得到了关于Dream的一个错误,并且用手破解了我的方式(回想起来,这是一个可怕的想法)。这非常令人沮丧,但我已经学到了很多东西!

相关问题