在 Rails 中显示嵌套资源的错误消息 [英] Showing error messages for nested resources in Rails

查看:50
本文介绍了在 Rails 中显示嵌套资源的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的第一个应用程序,简单的博客,但我不知道如何显示未通过验证的嵌套资源(评论)的错误消息.

这是评论的创建操作:

 def 创建@post = Post.find(params[:post_id])@comment = @post.comments.create(comment_params)重定向到 post_path(@post)结尾

这是评论表单:

 <%= form_for([@post, @post.comments.build]) 做 |f|%><p><%= f.label :commenter %><br/><%= f.text_field :commenter %></p><p><%= f.label :text %><br/><%= f.text_area :text %></p><p><%= f.submit %></p><%结束%>

我尝试过:

 def 创建@post = Post.find(params[:post_id])@comment = @post.comments.build(comment_params)如果@comment.save重定向到 post_path(@post)别的渲染'/comments/_form'结尾结尾

和:

 <% if @comment.errors.any?%><div id="error_explanation"><h2><%=pluralize(@comment.errors.count, "error") %>禁止此评论已保存:<ul><% @comment.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%>

但我不知道怎么回事.

解决方案

您无法从控制器渲染部分.更好的选择是创建一个 new 视图.

class CommentsController定义创建如果@comment.saveredirect_to post_path(@post),成功:'评论已创建'别的渲染:新结尾结尾结尾

app/views/comments/new.html.erb:

<% if @comment.errors.any?%><div id="error_explanation"><h2><%=pluralize(@comment.errors.count, "error") %>禁止此评论已保存:<ul><% @comment.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><%= 渲染部分:'form',评论:@comment %>

app/views/comments/_form.html.erb:

<%= form_for([@post, local_assigns[:comment] || @post.comments.build]) do |f|%><p><%= f.label :commenter %><br/><%= f.text_field :commenter %></p><p><%= f.label :text %><br/><%= f.text_area :text %></p><p><%= f.submit %></p><%结束%>

I am creating my first app, simple blog, and I don't know how to show error messages for nested resource(comments) that doesn't pass validation.

this is create action for comments:

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    redirect_to post_path(@post)
  end

this is comment form:

  <%= form_for([@post, @post.comments.build]) do |f| %>
    <p>
      <%= f.label :commenter %><br />
      <%= f.text_field :commenter %>
    </p>

    <p>
      <%= f.label :text %><br />
      <%= f.text_area :text %>
    </p>

    <p>
      <%= f.submit %>
    </p>
  <% end %>

I tried with:

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(comment_params)

    if @comment.save
      redirect_to post_path(@post)
    else
      render '/comments/_form'
    end
  end

and:

  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@comment.errors.count, "error") %> prohibited
        this comment from being saved:
      </h2>
      <ul>
        <% @comment.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

But I don't know whats wrong.

解决方案

You can't render a partial from a controller. A better alternative is just to create a new view.

class CommentsController
  def create
    if @comment.save
      redirect_to post_path(@post), success: 'comment created'
    else
      render :new
    end
  end
end

app/views/comments/new.html.erb:

<% if @comment.errors.any? %>
  <div id="error_explanation">
    <h2>
      <%= pluralize(@comment.errors.count, "error") %> prohibited
      this comment from being saved:
    </h2>
    <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>
<%= render partial: 'form', comment: @comment %>

app/views/comments/_form.html.erb:

<%= form_for([@post, local_assigns[:comment] || @post.comments.build]) do |f| %>
    <p>
        <%= f.label :commenter %><br />
        <%= f.text_field :commenter %>
    </p>

    <p>
        <%= f.label :text %><br />
        <%= f.text_area :text %>
    </p>

    <p>
        <%= f.submit %>
    </p>
<% end %>

这篇关于在 Rails 中显示嵌套资源的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆