我很新于Rails 3,并且已经跟随一些教程,现在我正在尝试“玩”创建的代码。 我已经按照http://guides.rubyonrails.org/getting_started.html中的教程进行了操作。
我正在尝试使用以下代码在主页上呈现新帖子的表单:
我正在尝试使用以下代码在主页上呈现新帖子的表单:
<%= render :partial => "posts/form" %>
posts/_form.html.erb的内容如下:
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我收到的错误信息:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for(@post) do |f| %>
2: <% if @post.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
Trace of template inclusion: app/views/home/index.html.erb
Rails.root: d:/server/cazare
Application Trace | Framework Trace | Full Trace
app/views/posts/_form.html.erb:1:in `_app_views_posts__form_html_erb___794893824_70478136_519766'
app/views/home/index.html.erb:5:in `_app_views_home_index_html_erb__967672939_70487520_0'
我知道这对你们中的一些人来说可能很简单,但我正在尝试理解Rails上的所有内容,所以希望你们能理解我。
提前感谢!
model_name
)。你需要查看错误信息,它应该会给出文件名和行号,然后贴出那一部分代码,并告诉我们具体发生在哪一行。 - Andrew Marshall