2016-09-01 3 views
1

저는 Ruby를 처음 사용합니다. 간단한 게시 응용 프로그램을 만드는 방법에 대한 지침을 따르려고합니다.Ruby on Rails가 작동하지 않는 동작을 생성합니다.

내 생성 동작이 작동하지 않습니다. 이 시도하고 터미널에서 뭔가를 할 것으로 보이지만 내 게시물 개체에 추가하지 않습니다. 내가 제출하려고 할 때

<h1>Add a New Post</h1> 

<%= form_for @post do |f| %> 
    <p> 
    <%= f.label :title %> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.label :content %> 
    <%= f.text_area :content %> 
    </p> 
    <p> 
    <%= f.submit "Add a New Post" %> 
    </p> 
<% end %> 

이 터미널에서 오는 것입니다 :

여기
class PostsController < ApplicationController 
    def index 
    @posts = Post.all 
    end 

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

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(:title => params[:title], :content => params[:content]) 
    @post.save 
    end 

    def edit 
    end 

    def update 
    end 

    def destroy 
    end 
end 

나의 새로운이다 : 여기

내 게시물 컨트롤러

Started POST "/posts" for ::1 at 2016-08-31 17:54:39 -0700 
ActiveRecord::SchemaMigration Load (16.4ms) SELECT "schema_migrations".* FROM   "schema_migrations" 
Processing by PostsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"tGpevHtpEoP5jHYqCn1G7tUKX9YWnx+PWkqlPzKadTCiIEX1UGs96mSCrDf UIShKjp+ObwNA6G1nh3KE5gAIgw==", "post"=>{"title"=>"Jack's Post", "content"=>"Please use this post"}, "commit"=>"Add a New Post"} 
(0.1ms) begin transaction 
SQL (16.0ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2016-09-01 00:54:40 UTC], ["updated_at", 2016-09-01 00:54:40 UTC]] 
(14.7ms) commit transaction 
No template found for PostsController#create, rendering head :no_content 
Completed 204 No Content in 114ms (ActiveRecord: 31.3ms) 

나는 이것에 관한 백만개의 스택 오버플로에 대한 글을 읽은 것 같아 아무도 대답을 얻지 못하는 것 같다. 어떤 도움을 주시면 감사하겠습니다!

답변

0

레코드를 데이터베이스에 성공적으로 삽입했습니다. 다음에 무엇을하고 싶니? 방법 :

redirect_to action: 'index' 
1

강력한 매개 변수를 사용하여 양식에서 필요한 매개 변수를 가져와야합니다. 당신이 당신의 소재 기존 솔루션이 작동하려면

class PostsController < ApplicationController 

    def create 
    @post = Post.new(post_params) 
    @post.save 
    end 

private 

    def post_params 
    params.require(:post).permit(:title, :content) 
    # params.require(:post).permit! # Allow all 
    end 

end 

,이 같은 PARAMS을 접두사해야합니다 : 당신이 로그를 살펴보면

@post = Post.new(:title => params[:post][:title], :content => params[:post][:content]) 

, 폼 입력이 내부에 중첩되어 나타납니다 post

"post"=>{"title"=>"Jack's Post", "content"=>"Please use this post"} 
+0

강한 매개 변수는 영업 이익이 따르는 튜토리얼에 포함되지 않을 수 있습니다. 나는 그들에 대해 들어 보지 못했다. (나는 여전히 Rails 3.x에있다.) 빨리가! – Mick

+0

그래서 두 가지 솔루션을 모두 제공했습니다. :) @MickSharpe – codyeatworld

+0

정말 고마워요! 그것은 효과가있다! 나는 이것이 내가하는 일을하는 이유를 이해할 수 없다는 것을 완전히 확신하지 못한다. 그러나 나는 계속해서 그것을 알아낼 것이다. –

0

로그를 보면 분명히 아무런 렌더링도 없다고 말합니다.

No template found for PostsController#create, rendering head :no_content 

그래서 우리가 어떤 행동을 리디렉션 할 필요가 PostsController#create 행동, 대부분 우리는 행동을 보여 리디렉션합니다. 따라서 액션을 만들 때 다음 줄을 추가해야합니다.

# redirects user to show page of newly created post. 
if @post.save 
redirect_to @post 
else 
render 'new' 
end 

이동 살인 파도 :

관련 문제