現在、「ドットインストール」というサイトを使用してrailsの学習を進めており、「作成した記事にコメントをする事が出来る」というアプリを作る学習をしているのですが「post」モデルと「coment」モデルを結びつけてコメント内容を「post」モデルに結びつけたいのですがエラーが発生してしまいます。
改善点が分かる方がいましたらアドバイスの方お願いします。
以下、エラー内容とソースコードになります!
不足しているもの等ありましたらご指摘の方お願いいたします!
ソースコードは以下の様になっています。
show.htmll.erb
<p><%= simple_format @post.body %></p> <h3>Comments</h3> <%= form_for([@post, @post.comments.build]) do |f| %> <p> <%= f.text_field :body %> </p> <p> <%= f.submit %> </p> <% end %> コード
index.html.erb
<%= link_to 'Add New', new_post_path, class: 'header-menu' %> My Posts </h2> <ul> <% @posts.each do |post| %> <li> <%= link_to post.title, post_path(post) %> <%= link_to '[Edit]', edit_post_path(post), class: 'comand' %> <%= link_to '[batu]', post_path(post), method: :delete, class: 'comand', data: { confirm: 'Sure?' } %> </li> <% end %> </ul> コード
controller
class PostsController < ApplicationController def index @posts = Post.all.order(created_at: 'desc') end def show @post = Post.find(params[:id]) end def new @post = Post.new end def create #render plain: params[:post].inspect #@post = Post.new(params.require(:post).permit(:title, :body)) @post = Post.new(post_params) if @post.save redirect_to posts_path else #render plain: @post.errors.inspect render 'new' end end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to posts_path else render 'edit' end end def destroy @post = Post.find(params[:id]) @post.destroy redirect_to posts_path end private def post_params params.require(:post).permit(:title, :body) end end コード
ルーティング
Prefix Verb URI Pattern Controller#Action post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PATCH /posts/:post_id/comments/:id(.:format) comments#update PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update syota0618:~/workspace $ cd myblog syota0618:~/workspace/myblog (master) $ rails routes Prefix Verb URI Pattern Controller#Action post_comments GET /posts/:post_id/comments(.:format) comments#index POST /posts/:post_id/comments(.:format) comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) comments#show PATCH /posts/:post_id/comments/:id(.:format) comments#update PUT /posts/:post_id/comments/:id(.:format) comments#update DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy root GET / コード
commentモデル
class Coment < ApplicationRecord belongs_to :post validates :body, presence: true end コード
postモデル
class Post < ApplicationRecord has_many :comments # @post.coments #validates :title, presence: true, length:{ minimum: 3, message: 'タイトルを入力して下さい'} validates :body, presence: true end コード
回答1件
あなたの回答
tips
プレビュー