前提・実現したいこと
railsで新規投稿を保存したい
発生している問題・エラーメッセージ
Routing Error No route matches [POST] "/posts" Rails.root: /Users/matsuyamatakeshi/Desktop/pf1 Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom
該当のソースコード
ruby
1routes.rbファイル 2 3Rails.application.routes.draw do 4# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 5 resources :posts, only: %i[new index show create] 6end
ruby
1posts.controller.rbファイル 2 3class PostsController < ApplicationController 4 5 def index 6 @posts = Post.all 7 end 8 9 def new 10 @post = Post.new 11 end 12 13 def show 14 @post = Post.find(params[:id]) 15 end 16 17 def create 18 Post.create(post_params) 19 end 20 21 private 22 23 def post_params 24 params.require(:Post).permit(:name, :title, :body) 25 end 26end
ruby
1new.html.erbファイル 2 3<div class="d-flex align-items-center py-3"> 4 <h1>新規投稿</h1> 5 <div class="ml-auto boards__linkBox"> 6 <div class="btn btn-light"><%= link_to '戻る', posts_path %></div> 7 </div> 8</div> 9<%= form_for @post do |p| %> 10<div class="form-group newitem1"> 11 <%= p.label :name, '名前' %> 12 <%= p.text_field :name, class: 'form-control' %> 13</div> 14<div class="form-group newitem1"> 15 <%= p.label :title, '遺産名' %> 16 <%= p.text_field :title, class: 'form-control' %> 17</div> 18<div class="form-group newitem1"> 19 <%= p.label :body, '本文' %> 20 <%= p.text_area :body, class: 'form-control', rows: 10 %> 21</div> 22<%= p.submit '保存', class: 'btn btn-primary' %> 23<% end %>
ruby
1post.rbファイル 2 3# == Schema Information 4# 5# Table name: posts 6# 7# id :integer not null, primary key 8# body :text 9# name :string 10# title :string 11# created_at :datetime not null 12# updated_at :datetime not null 13# 14 15class Post < ApplicationRecord 16end 17
試したこと
補足情報(FW/ツールのバージョンなど)
Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。