前提・実現したいこと
現在掲示板アプリを作成しています。
今まで順調に進んできてform関連も問題なく使えていたのに急にform関連が使えなくなりました。
発生している問題
発生している問題として新規投稿やコメントといった機能を使っても何も反応せずにページの遷移もしなくなりました。
該当のソースコード
post_controller.rb
class PostsController < ApplicationController def index @posts = Post.all.order(created_at: :desc) @posts = Post.paginate(page: params[:page], per_page: 10) end def show @post = Post.find(params[:id]) end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to @post else 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 @post 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, :text) end end
_form.html.erb
<form> <div class="container"> <%= form_with model:@post, local:true do |f| %> <div class="field"> <div class="control"> <strong> <%= f.label :タイトル %> </strong> <%= f.text_field :title, class: 'form-control' %> </div> </div> <div class="field"> <div class="control"> <strong> <%= f.label :内容 %> </strong> <%= f.text_area :text, class:"form-control", rows: 10 %> </div> </div> <%= f.submit :Submit, class: "btn btn-dark click"%> <% end %> </div> </form>
index.html.erb
<div class="wrapper"> <div class="container"> <h1 class="header">掲示板</h1> <button type="button" class="btn btn-info"> <%= link_to "スレッド新規作成", new_post_path, class: "new" %> </button> <button type="button" class="btn btn-info "> <%= link_to "ユーザー一覧", '#', class: "new" %> </button> <hr /> <%= will_paginate %> <div class="section"> <% @posts.each do |post| %> <div class=" title"> <%= link_to post.title, post_path(post) %> <div class=" destroy"> <%= link_to "削除", post_path(post), method: :delete, data: {confirm: "本当にいいですか?"}, class: "btn btn-danger" %> </div> </div> <% end %> </div> <%= will_paginate %> </div> </div>
comments/_form.html.erb
<form> <%= form_with(model: [ @post, @post.comments.build ], local: true) do |f| %> <div class="field"> <div class="control"> <%= f.label :名前 %><br> <%= f.text_field :commenter,class: 'form-control' %> </div> </div> <div class="field"> <div class="control"> <%= f.label :コメント %><br> <%= f.text_area :body,class: 'form-control', rows: 7 %> </div> </div> <%= f.submit :Submit, class: "btn btn-dark click"%> <% end %> </form>
comments_controller.rb
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params) redirect_to post_path(@post) end def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@post) end private def comment_params params.require(:comment).permit(:commenter, :body) end end
原因として考えられること
今までは問題なく動いていたのですがrails db:seedsを起動したり、ページネーション機能を追加して実装したら動かなくなったのでこれらに原因があるのかと考えていますが、他のコードをあまりいじっていないため結局のところどこが直接問題なのかがわかりません。
是非手助けお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。