I want to show errors when they are in form and can not understand why this code does not work. hotel.rb
class Hotel < ActiveRecord::Base
...
has_many :comments
...
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :hotel
belongs_to :user
delegate :email, to: :user, prefix: true
validates :body, presence: true, length: { minimum: 5, maximum:200 }
end
hotels/show.
...
%h2 Comments
#comments
.ui.piled.blue.segment
.ui.header
%i.icon.inverted.circular.blue.comment
Comments
.ui.comments
= render :partial => @hotel.comments
= render 'comments/form', comment: @hotel.comments
...
_form
-if user_signed_in?
= simple_form_for [@hotel, Comment.new] do |f|
=f.error_notification
%br
.ui.reply.form
.field
=f.label :body, "New comment"
=f.input :body, as: :text, label: false
=f.submit 'Add comment', class: "ui fluid blue labeled submit icon button"
-else
=link_to 'Sign in to add comment', new_user_session_path, class: 'ui blue button'
_comment
= div_for comment do
.comment
.content
%span.author= comment.user_email
.metadata
%span.date Posted #{time_ago_in_words(comment.created_at)} ago
.text
= comment.body
If you add too_short and too_long model that does not correct the.
UPDATE
comments_controller
class CommentsController < ApplicationController
def create
@hotel = Hotel.find(params[:hotel_id])
@comment = @hotel.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.save
redirect_to @hotel
end
private
def comment_params
params.require(:comment).permit(:user_id, :body, :hotel_id)
end
end
I solved this problem.
comments_controller
def create
@hotel = Hotel.find(params[:hotel_id])
@comment = @hotel.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.save
respond_to do |format|
if @comment.save
format.html { redirect_to @hotel }
else
format.html { render partial: 'comments/form' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
_form
-if user_signed_in?
= simple_form_for [@hotel, @comment] do |f|
- if @comment.errors.any?
#error_explanation
%h2
= pluralize(@comment.errors.count, "error")
prohibited this comment from being saved:
%ul
- @comment.errors.full_messages.each do |msg|
%li= msg
%br
.ui.reply.form
=f.error_notification
.inputs
=f.label :body, "New comment"
=f.input :body, as: :text, label: false
.actions
=f.button :submit, 'Add comment', class: "ui fluid blue labeled submit icon button"
-else
=link_to 'Sign in to add comment', new_user_session_path, class: 'ui blue button'
%br
This code render comment form when the comment doesn't save, and write the errors.
Your issue is in your controller. Error's won't populate until you try and save the record. Well in your instance you don't have any flow control to see if it saved, you just have @comment.save
. If it doesn't save or if it does save, you do the same redirect.
Try this:
if @comment.save
redirect_to @hotel, notice: 'Comment was successfully created.'
else
redirect_to @hotel, notice: 'There was an issue trying to save your comment.'
end
Now with render :new
, @comment.errors
will be populated (assuming you tried an invalid length) and now you should see the error messages displayed!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.