解決したい内容
railsにて投稿機能の作成中に発生した以下のエラーを解決したいです。
![
試したこと
こちらの記事を参考にし、@postsが空でないかを確認するため@posts を使っている部分をすべてコメントアウトして、<%= @posts.nil? %>
をviewで確認したところ、falseが返ってきたため、その段階では空ではないと自分の中で判断しました。
しかしながら、posts_controller.rb
のindexアクション
内にて、@posts
のすぐ下にp @posts
を記述し、ターミナルで出力を確認したところ
![
上の画像のように、#<ActiveRecord::Relation []>
のように値が空で返ってきています。
この後、解決策を調べてみましたが、いい案は見つからず、コードの誤字脱字等も見ましたが見つかりませんでした。
何卒、解決策をご教授お願い致します。
コード
_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 render :index 17 end 18 end 19 20 private 21 def post_params 22 params.require(:post).permit(:title,:image_name,:content) 23 end 24 25end
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
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/17 06:23