#やりたいこと
railsの新規投稿画面で、エラーメッセージのための[]が出ないようにしたいです。
バリデーションを設定し、flash[:notice]でエラーメッセージを出すようにしていますが、何も入力していない新規投稿の画面で[]が出てしまいます。
#現在のコード
new.html.erb↓
html
1<h1>post</h1> 2<%= form_tag('/blog', method: :post) do %> 3 <div class="new-post-submit"> 4 <%= @post.errors.full_messages do |message| %> 5 <%= message %> 6 <% end %> 7 <textarea name="title" rows="1" cols="80"></textarea> 8 <textarea name="content" rows="8" cols="80"></textarea> 9 <input type="submit" value="submit"> 10 </div> 11<% end %>
posts.controller↓
ruby
1 def new 2 @post = Post.new(title: params[:title], content: params[:content]) 3 end 4 5 def create 6 params[:title] 7 params[:content] 8 @post = Post.new(title: params[:title], content: params[:content]) 9 if @post.save 10 flash[:notice] 11 redirect_to("/blog") 12 else 13 render("posts/new") 14 end 15 end
post.rb↓
ruby
1class Post < ApplicationRecord 2 validates :title, {presence: true, length: {maximum: 60}} 3 validates :content, {presence: true} 4end
わかる方がいらっしゃいましたらご教示いただきたいです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/23 02:15
2019/09/23 04:07