解決したいこと
作成中のrailsアプリにて投稿機能を作っており、モーダルformで投稿した値が保存されず、posts_controller.rbにおいて、createアクションのrender側の処理に行ってしまいます。
原因が掴めないため、何卒ご教授のほどよろしくお願い致します。
投稿が保存されず、 render :indexの処理に行ってしまう
_post_form.html.erb
html
1<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> 2 <div class="modal-dialog" role="document"> 3 <div class="modal-content"> 4 5 <div class="modal-header"> 6 <h5 class="modal-title" id="exampleModalLabel">New Post</h5> 7 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 8 <span aria-hidden="true">×</span> 9 </button> 10 </div> 11 12 <%= form_with model: Post.new, local: true do |form| %> 13 <div class="modal-body"> 14 <div class="form-group"> 15 <%= form.label :title, 'Title:' %> 16 <%= form.text_field :title, class: 'form-control'%> 17 </div> 18 <div class="form-group"> 19 <div class="field_image"> 20 <div id="img_field" onClick="$('#file').click()"> 21 <%= image_tag("/pic_images/default_image.jpg") %> 22 </div> 23 <%= form.file_field :image_name, class: "image", style: "display:none;", id: "file"%> 24 </div> 25 </div> 26 <div class="form-group"> 27 <%= form.label :content, 'content:' %> 28 <%= form.text_area :content, class: 'form-control' , rows:3%> 29 </div> 30 </div> 31 <div class="modal-footer"> 32 <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> 33 <%= form.submit 'post', class: 'btn btn-primary' %> 34 </div> 35 <% end %> 36 37 </div> 38 </div> 39</div>
routes.rb
ruby
1Rails.application.routes.draw do 2 resources :posts, only: [:index,:create] 3 resources :users, only: [:create,:edit,:update] 4 post 'login', to: 'users#login' 5 post 'logout', to: 'users#logout' 6 root 'home#top' 7 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 8end
posts_controller.rb
ruby
1class PostsController < ApplicationController 2 3 4 def index 5 @posts = Post.page(params[:page]).per(4).order(created_at: :desc) 6 p @posts 7 end 8 9 def create 10 @post = Post.new(post_params) 11 if @post.save 12 @post.user_id = @current_user.id 13 flash[:success] = "投稿を作成しました" 14 redirect_to posts_path 15 else 16 @posts = Post.page(params[:page]).per(4).order(created_at: :desc) 17 render :index 18 end 19 end 20 21 private 22 def post_params 23 params.require(:post).permit(:title,:image_name,:content) 24 end 25 26end 27
index.html.erb
html
1<div class="posts-index"> 2 <div class="container"> 3 4 <%= render partial: 'post_form' %> 5 <%= render partial: 'user_modal'%> 6 7 <div class="post_card jscroll"> 8 <% @posts.each do |post| %> 9 <section class="card"> 10 <div class="card-content"> 11 <div class="user"> 12 <div class="card-user_image user_item"><%= image_tag post.user.image_name.to_s %></div> 13 <p class="user_item"><%= post.user.name %></p> 14 </div></br> 15 <p class="post_title"><%= post.title %></p> 16 <div class="post_image"><%= image_tag post.image_name.to_s %></div></br> 17 <p class="post_content"><%= simple_format(post.content) %></p> 18 </div> 19 </section> 20 <% end %> 21 <%= paginate @posts %> 22 </div> 23 24 </div> 25</div>
バージョン
rails 5.2.3
ruby 2.6.3
回答1件
あなたの回答
tips
プレビュー