【发布时间】:2015-05-02 19:37:53
【问题描述】:
在尝试在索引中渲染新的 simple_form 时。我已经关注 http://guides.rubyonrails.org/v2.3.11/layouts_and_rendering.html ,在 2.2.2 渲染操作的视图中,我在 Posts#index 中收到错误 NoMethodError ,未定义的方法 `model_name' 用于 NilClass:Class
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!,except:[:index,:show]
def index
@posts = Post.all.order("created_at DESC")
render 'new'
end
def new
@post = current_user.posts.build
end
index.html.haml
- if user_signed_in?
= link_to "New Post", new_post_path
- @posts.each do |post|
%h2.post= link_to post.post, post
%h4.post= link_to post.location, post
%h4.post= link_to post.tag_list, post
%p.date
Published at
= time_ago_in_words(post.created_at)
by
= post.user.email
_form.html.haml
= simple_form_for @post do |f|
= f.input :post
= f.input :location
= f.input :tag_list
= f.input :active
= f.submit
new.html.haml
%h1 Post
= render 'form'
【问题讨论】:
-
你能发布日志吗?
-
@ahmadhamza 对不起什么日志?新的 Rails ..
-
日志表示你的终端输出。
-
=> Post(id: integer, post: text, location: string, tag_list: string, active: boo lean, created_at: datetime, updated_at: datetime, user_id: integer)
-
NilClass:Class 意味着你得到一个空数组。你能从终端粘贴确切的日志吗?它确实提到了行号和文件名。
标签: ruby-on-rails forms ruby-on-rails-4 activerecord simple-form