我是 Rails 的新手,我想在没有脚手架的情况下做到这一点,所以我真的会学习它。我查看了 railstutorial.org,但它与我的项目正在做的不同,所以我在另一个 rails 项目中生成了脚手架并复制编辑过的代码。
环境:Ubuntu Lucid,ruby 1.9.3p125,rails 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' %>
应用程序/视图/梦想/_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
我已经用谷歌搜索了几个小时,但无法弄清楚。我会很感激任何帮助!