2016-08-12 2 views
0

게시물을 저장하려고 할 때마다이 오류가 표시됩니다.NoMethodError in PostsController # 정의되지 않은 메서드 'body'를 만듭니다.

Image

여기 내 new.html.erb 파일입니다.

<div id="page_wrapper"> 
<h1>New Post</h1> 
    <%= form_for :post, url: posts_path do |f| %> 
    <% if @post.errors.any? %> 
    <div id="errors"> 
    <h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving:</h2> 
    <ul> 
     <% @post.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
    </div> 
<% end %> 
<p> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %><br> 
</p> 

<p> 
    <%= f.label :date %><br> 
    <%= f.text_field :date %><br> 
</p> 

<p> 
    <%= f.label :time %><br> 
    <%= f.text_field :time %><br> 
</p> 

<p> 
    <%= f.label :location %><br> 
    <%= f.text_field :location %><br> 
</p> 

<p> 
    <%= f.submit %> 
</p> 
<% end %> 
</div> 

여기 내 show.html.erb 파일입니다.

<div id="page_wrapper"> 
<h1 class="title"> 
    <%= @post.title %> 
</h1> 

<p class="date"> 
    Submitted <%= time_ago_in_words(@post.created_at) %> Ago 
</p> 

<p class="time"> 
    <%= @post.time %> 
</p> 

<p class="location"> 
    <%= @post.location %> 
</p> 
</div> 

여기 내 posts_controller.rb 파일입니다.

class PostsController < ApplicationController 
def index 
    @posts = Post.all.order('created_at DESC')  
end 

def new  
    @post = Post.new  
end 

def create 
    @post = Post.new(post_params) 

    if @post.save 
     redirect_to @post 
    else 
     render 'new' 
    end 
end 

def show 
    @post = Post.find(params[:id])  
end 

private 
def post_params 
    params.require(:post).permit(:title, :body, :location) 
end 
end 

여성용의 내 routes.rb 파일

Rails.application.routes.draw do 
    resources :posts 
    root "posts#index" 
end 
+0

당신의 schema.rb –

+0

인사를 공유, 액티브 :: Schema.define (버전 : 20160812052026)는 힘을 CREATE_TABLE "게시물"을 수행합니다 : 캐스케이드 할 | t을 | t.string "제목" t.string "날짜" t.string "시간" t.text "위치" t.datetime "created_at", 널 (null) : 거짓 t.datetime "updated_at", 널 (null) : 거짓 끝 끝 – Bikal

+0

@ArunKumar 내가 내 위의 오류 스크린 샷을 가지고, 그것은 나 제공 NoMethodError가 PostsController에서 #는 #에 대한 <포스트 : 0x007f3b81a787f8>을 정의되지 않은 메서드'몸 '을 만들어이 게시물 – Bikal

답변

1

당신은 당신의 post.rbbody에 대한 유효성 검사를 추가했습니다. 그러나 테이블에 body 필드가 없습니다. @post.create가 호출 될 때

그래서, post.rb에 정의 된 모든 검증을 실행하고 당신이 body 필드를 가지고 있지 않기 때문에, 당신은 Undefined method 'body' for Post 오류가 발생했습니다. 그것을 해결하기

는 다음 줄

validates :body, presence: true 

를 제거 post.rb

body에 대한 검증을 제거하고 당신이 가서 잘되어야합니다. 내 schema.rb는 다음과 같습니다 @pavan

0
params.require(:post).permit(:title, :body, :location) 

당신은 :body이 필요하지만 형태는 title, date, time and location 있습니다.

을 제거하고이이처럼 강한 PARAMS 속성을 가지고 있는지 확인하십시오

params.require(:post).permit(:title, :date, :time, :location) 
+0

여전히 작동하지 않는 동일한 문제 : ( – Bikal