Browse#home中的NoMethodError

时间:2014-05-13 03:54:22

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

我在browse / home

中有以下视图文件
<% if current_user %>
<% @post = Post.new %>
<%=render :partial => 'posts/newpost.html.erb'%>
<div id="postsfeed">
<%= render :partial => 'post.html.erb', :locals => { :posts_streams => @posts_streams } %>
</div></br>
<% end %>

posts / newpost.html.erb是

<%= form_for(@post) do |f| %>
<div class="fields">
<%= f.label "Post" %><br />
<%= f.text_field :post %>
</div>
<div class="actions" id="refreshposts">
<%= f.submit("Post") %>
</div>
<% end %>

models / post.rb如下

class Post < ActiveRecord::Base
attr_accessible :post, :posted_by, :posted_by_uid
end

日志:

Rendered browse/home.html.erb within layouts/application (42.9ms)
Completed 500 Internal Server Error in 72ms

ActionView::Template::Error (undefined method `post' for #<Post:0x00000003ad7cf0>):
1: <%= form_for(@post) do |f| %>
2: <div class="fields">
3:     <%= f.label "Post" %><br />
4:     <%= f.text_field :post %>
5:   </div>
6: <div class="actions" id="refreshposts">
7:     <%= f.submit("Post") %>
app/views/posts/_newpost.html.erb:4:in `block in _app_views_posts__newpost_html_erb__281623928071728826_30583780'
app/views/posts/_newpost.html.erb:1:in `_app_views_posts__newpost_html_erb__281623928071728826_30583780'
app/views/browse/home.html.erb:3:in `_app_views_browse_home_html_erb___3611275753955382446_40484320'

当我启动服务器时。它在Browse#home中返回NoMethodError。请提出解决此问题的方法

谢谢

2 个答案:

答案 0 :(得分:0)

可能在数据库

中的帖子表中应该有一个帖子列

根据[Rails标准](http://guides.rubyonrails.org/layouts_and_rendering.html#Using Partials),还应注意以下几点:

避免提及扩展名.html.erb,因此请更改以下代码:

<%= render :partial => 'posts/newpost.html.erb'%> 

<%= render :partial => 'post.html.erb', :locals => { :posts_streams => @posts_streams } %>

为:

<%= render :partial => 'posts/newpost'%> 

<%= render :partial => 'post', :locals => { :posts_streams => @posts_streams } %>

此外,部分以下划线开头,因此将视图文件夹中的部分放置为posts / _newpost.html.erb。

答案 1 :(得分:-1)

检查您的路线并添加

 resources posts

或添加部分参与示例您的文件是'_partial.html.erb'

   <%= render controller/partial %>

模板错误是告诉您没有要呈现的页面的错误,因此请检查它是否存在于同一路径中。