新規投稿画面を作ろうとRailsを学んでいましたが
なぜかブラウザー表示でエラーが発生します。
/sample_app/app/views/posts/new.html.erb
HTML(rails)
1<h1>新規投稿</h1> 2<!-- form_for部分 --> 3 <%= form_for(@post) do |f| %> 4 5 <h4>タイトル</h4> 6 <%= f.text_field :title %> 7 8 <h4>本文</h4> 9 <%= f.text_area :body %> 10 11 <%= f.submit '投稿' %> 12 13 <% end %>
/sample_app/config/routes.rb
Ruby
1Rails.application.routes.draw do 2 get 'posts/new' 3 4 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 5 6 get '/top' => 'root#top' 7 8 post '/posts' => 'posts#create' 9 10en
/sample_app/app/controllers/posts_controller.rb
ruby
1class PostsController < ApplicationController 2 3 def new 4 @post = Post.new 5 end 6 7 # 以下を追加 8 def create 9 # ストロングパラメーターを使用 10 post = Post.new(post_params) 11 # DBへ保存する 12 post.save 13 # 新規投稿画面へリダイレクト 14 redirect_to '/posts/new' 15 end 16 17 private 18 19 def post_params 20 params.require(:post).permit(:title, :body, :category) 21 end 22end
/sample_app/db/migrate/20180222080918_create_posts.rb
Ruby
1class CreatePosts < ActiveRecord::Migration[5.1] 2 def change 3 create_table :posts do |t| 4 # 以下の2行を追加 5 t.string :title 6 t.string :body 7 t.timestamps 8 end 9 end 10end
どこがいけないのかがわかりません。
ご教示お願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。