Ruby on rails 错误不显示/Rails,简单形式

Ruby on rails 错误不显示/Rails,简单形式,ruby-on-rails,ruby,simple-form,Ruby On Rails,Ruby,Simple Form,我想在错误出现在表单中时显示错误,并且无法理解为什么这段代码不起作用。 hotel.rb class Hotel < ActiveRecord::Base ... has_many :comments ... end class Comment < ActiveRecord::Base belongs_to :hotel belongs_to :user delegate :email, to: :user, prefix: true validates :b

我想在错误出现在表单中时显示错误,并且无法理解为什么这段代码不起作用。 hotel.rb

class Hotel < ActiveRecord::Base
...
  has_many :comments
...
end
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
\u表单

-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'
-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
\u评论

= 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
如果您添加的模型太短或太长,则无法纠正错误

更新

评论\u控制器

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
 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
class CommentsController
您的问题在控制器中。在您尝试保存记录之前,不会填充错误。在您的实例中,您没有任何流控件来查看它是否保存,您只有
@comment.save
。如果它不保存或确实保存,则执行相同的重定向

试试这个:

  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

现在使用
render:new
@comment.errors
将被填充(假设您尝试了无效长度),现在您应该可以看到显示的错误消息了

您的问题在控制器中。在您尝试保存记录之前,不会填充错误。在您的实例中,您没有任何流控件来查看它是否保存,您只有
@comment.save
。如果它不保存或确实保存,则执行相同的重定向

试试这个:

  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

现在使用
render:new
@comment.errors
将被填充(假设您尝试了无效长度),现在您应该可以看到显示的错误消息了

您的问题在控制器中。在您尝试保存记录之前,不会填充错误。在您的实例中,您没有任何流控件来查看它是否保存,您只有
@comment.save
。如果它不保存或确实保存,则执行相同的重定向

试试这个:

  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

现在使用
render:new
@comment.errors
将被填充(假设您尝试了无效长度),现在您应该可以看到显示的错误消息了

您的问题在控制器中。在您尝试保存记录之前,不会填充错误。在您的实例中,您没有任何流控件来查看它是否保存,您只有
@comment.save
。如果它不保存或确实保存,则执行相同的重定向

试试这个:

  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
现在使用
render:new
@comment.errors
将被填充(假设您尝试了无效长度),现在您应该可以看到显示的错误消息了

我解决了这个问题

评论\u控制器

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
 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
\u表单

-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'
-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
此代码在注释未保存时呈现注释表单,并写入错误。

我解决了此问题

评论\u控制器

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
 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
\u表单

-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'
-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
此代码在注释未保存时呈现注释表单,并写入错误。

我解决了此问题

评论\u控制器

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
 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
\u表单

-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'
-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
此代码在注释未保存时呈现注释表单,并写入错误。

我解决了此问题

评论\u控制器

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
 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
\u表单

-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'
-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

此代码在注释未保存时呈现注释表单,并写入错误。

控制器中有什么?你能把它也贴出来吗?你的控制器里有什么?你能把它也贴出来吗?你的控制器里有什么?你能把它也贴出来吗?你的控制器里有什么?你也可以发布吗?如果我使用
重定向到@hotel
comment,我没有新的评论模板。错误会起作用吗?对于创建评论,我使用部分(_表单)我没有新的评论模板,如果我使用
重定向到@hotel
comment.errors会起作用吗?对于创建评论,我使用部分(_表单)我没有新的评论模板,如果我使用
重定向到@hotel
comment.errors会起作用吗?对于创建评论,我使用部分(_表单)我没有新的评论模板,如果我使用
重定向到@hotel
comment.errors会起作用吗?对于创建评论,我使用部分(_形式)这正是我所说的-请接受我的答案。这正是我所说的-请接受我的答案。这正是我所说的-请接受我的答案。这正是我所说的-请接受我的答案。