gpt4 book ai didi

ruby-on-rails - 如何渲染部分表单的验证错误 - Rails

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:58 25 4
gpt4 key购买 nike

我正在构建一个自定义博客。我有 postscomments

我通过对单个帖子的显示操作的部分呈现评论。

帖子 Controller

 class Blog::PostsController < Blog::BaseController

def show
@post = Post.find_by_permalink(params[:id])
@comment = Comment.new(:post => @post)
end

end

评论 Controller

class Blog::CommentsController < Blog::BaseController

def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:success] = "Comment successfully created!"
redirect_to blog_post_url(@comment.post)
else
flash[:warning] = "Something went wrong, try again. If problem persists please let our team know about it!"
redirect_to :back
end
end

private
def comment_params
params.require(:comment).permit(:body,:name,:email,:user_id,:post_id)
end
end

show.html.erb

<div class="row post-container">
<div class="large-offset-1 large-7 medium-12 columns post-content">
<h1 class="post-title"> <%= link_to @post.title, blog_post_path(@post) %> </h1>
<p class="published-date"><em>Published on <%= l @post.published_at, format: :date %></em></p>

<div class="post-body">
<%= @post.body.html_safe %>
<%= render partial: "blog/comments/comment", post: @post %>
</div>
</div>
<div class="large-4 columns sidebar">
sidebar
</div>
</div>

评论部分形式

<%= form_for [:blog,@comment] do |f| %>
<%= render 'blog/shared/error_messages', object: f.object %>

<div class="field panel">
<%= f.label :name %><br>
<%= f.text_field :name,class: 'form-control' %>
</div>

<div class="field panel">
<%= f.label :email %><br>
<%= f.text_field :email,class: 'form-control' %>
</div>

<div class="field panel">
<%= f.label :body, "Comment" %><br>
<%= f.text_area :body,class: 'form-control' %>
</div>

<% if logged_in? %>
<%= f.hidden_field :user_id, value: current_user.id %>
<% end %>

<%= f.hidden_field :post_id %>



<div class="actions">
<%= f.submit "Create comment", class:"btn btn-danger btn-block" %>
<a class="btn btn-warning btn-block cancel">Cancel</a>
</div>
<% end %>

部分错误消息

<% if object.errors.any? %>
<div id="error_explanation">
<div class="errors-alert text-center">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li class="errors-alert-item text-center"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

我尝试对评论模型进行验证,但它不会显示它们,即使它正确地重定向回来也是如此。

我知道我必须使用渲染而不是重定向,但我不知道要渲染什么。这是我想弄清楚的,因为我没有渲染新 Action 。

最佳答案

重写您的 Blog::CommentsController Controller create 操作,以便它重新呈现表单而不是重定向到上一页。这样,错误将呈现在页面上。

class Blog::CommentsController < Blog::BaseController

def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:success] = "Comment successfully created!"
redirect_to blog_post_url(@comment.post)
else
flash[:warning] = "Something went wrong, try again. If problem persists please let our team know about it!"
@post = @comment.post
render 'blog/post/show'
end
end

end

通过这种方式,您可以重新呈现 CommentsController 中的 show 操作,并明确为其提供正确的 @post 以便它可用在 View 中。

关于ruby-on-rails - 如何渲染部分表单的验证错误 - Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com