前提・実現したいこと
ここに質問の内容を詳しく書いてください。
ページの上段にエラーメッセージを表示したいと考えています。
先ほどから色々と試しているのですが、エラーが出るため、なかなか進展いたしません。
よろしければお力お貸しください。
よろしくお願いします。
学習を始めたばかりで間違いが多いかもしれません。申し訳ありません。
ruby
1<!-- ここからエラーメッセージのコードです。 (index.html.erb) --> 2<% if Book.errors.any? %> 3 <%= Book.errors.count %>errors prohibited this obj from being saved:<br> 4 <% Book.errors.full_messages.each do |message| %> 5 <%= message %> 6 <% end %> 7<% end %> 8<!-- ここまでがエラーメッセージのコードです。 --> 9<div class="container"> 10<div class="row"> 11 <div class="col-xs-3"> 12 <h3 class="#">User info</h3> 13 <table class="table"> 14 <thead> 15 <tr><%= attachment_image_tag @user, :profile_image, size:"100x100", format: 'jpeg', fallback: "no_image.jpg" %></tr> 16 </thead> 17 <thead> 18 <tr> 19 <th>name</th> 20 <th><%= @user.name %></th> 21 </tr> 22 </thead> 23 <tr> 24 <th>introduction</th> 25 <th><%= @user.introduction %></th> 26 </tr> 27 <% if @user.id == current_user.id %> 28 <th> 29 <%= link_to edit_user_path(@user), class:"btn btn-default btn-lg btn-block" do%> 30 <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> 31 </button> 32 <% end %> 33 <% end %> 34 </table> 35 36 <h2>New Book</h2></br> 37<%= form_for(Book.new) do |book| %> 38<h4>Title</h4> 39<%= book.text_field :title %> 40</br> 41<h4>opinion</h4> 42<%= book.text_area :body %> 43</br> 44<%= book.submit "Create Book" %></br> 45<% end %> 46</div> 47 48<div class="container"> 49 <div class="row"> 50 <div class="col-xs-9"> 51 <h3>Books</h3> 52<table class="table"> 53 <thead> 54 <th></th> 55 <th>title</th> 56 <th>opinion</th> 57 </thead> 58 <tbody> 59 <% @books.each do |book| %> 60 <tr> 61 <td> 62 <%= attachment_image_tag book.user, :profile_image, size:"20x20", format: 'jpeg', fallback: "no_image.jpg" %> 63 </td> 64 <td> 65 <%= link_to book_path(book.id) do %> 66 <%= book.title %> 67 <% end %> 68 </td> 69 <td> 70 <%= book.body %> 71 </td> 72 </tr> 73 <tr> 74 </tr> 75 <% end %> 76 <tbody> 77 <tr> 78 79 </tr> 80 </tbody> 81</table> 82 83</div> 84</div> 85</div> 86 87</div> 88</div> 89
ruby
1class BooksController < ApplicationController 2before_action :authenticate_user! 3 def show 4 # @book = Book.new 5 @book = Book.find(params[:id]) 6 end 7 8 def create 9 @book = Book.new(book_params) 10 @book.user_id = current_user.id 11 if @book.save 12 redirect_to book_path(@book.id), notice: "You have creatad book successfully." 13 else 14 redirect_to books_path 15 end 16 end 17 18 def index 19 @books = Book.all 20 @users = User.all 21 @user = current_user 22 end 23 24 def edit 25 @book = Book.find(params[:id]) 26 if 27 @book.user.id != current_user.id 28 redirect_to books_path 29 end 30 end 31 32 def update 33 @book = Book.find(params[:id]) 34 if @book.update(book_params) 35 redirect_to book_path, notice: "You have updated user successfully.." 36 else 37 render action: :edit 38 end 39 end 40 41 def destroy 42 @book = Book.find(params[:id]) 43 @book.destroy 44 redirect_to books_path 45 end 46 47 48private 49 def book_params 50 params.require(:book).permit(:title, :body, :user_id) 51 end 52 53end 54
ruby
1class Book < ApplicationRecord 2 belongs_to :user 3 attachment :profile_image 4 validates :title, presence: true 5 validates :body, presence: true 6 validates :body, length: { maximum: 200 } 7end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。