私はレールのブログで簡単なルビーに取り組んでいます。ユーザーが投稿を送信すると、投稿ページにリダイレクトされます。リダイレクトされる代わりに、NoMethodError in PostsController#create
- undefined method 'post_path' for #<PostsController:0x007ff379d72110> Did you mean? posts_path post_params root_path
エラーが発生しています。投稿ページにリダイレクトされない投稿
post_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user!
def new
@post = Post.new
end
def create
@post = current_user.posts.new(post_params)
if @post.save
redirect_to post_path(@post)
else
render :new
end
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body, :all_tags)
end
end
show.html.erb
<h1 class="show_title">
<%[email protected]%>
</h1>
<p class="show_date">submiited <%= time_ago_in_words(@post.created_at) ago%></p>
<p class="show_body"><%[email protected]%></p>
routes.rbを
Rails.application.routes.draw do
devise_for :users
root 'homes#show'
resources :users, only: [:show, :edit, :update]
resources :posts, only: [:new, :create]
resource :relationships, only: [:create, :destroy]
あなたのroutes.rbファイルにあるものを表示する必要があります – fanta
@fantaがルートを追加しました。 – DavidK
私は 'show'アクションにリダイレクトしようとしていますが、' new'と 'create'のルートを定義しただけで' show'もそこに追加しています: 'resources:posts、only:[new 、:作成、:表示] ' – fanta