将变量从控制器传递到视图

发布于 2024-12-27 11:28:39 字数 1409 浏览 0 评论 0原文

我在 Rails 上写了一个简单的博客。 我有一个帖子模型和一个评论模型。 当您创建评论时,如果评论无效,我想显示错误。 我该怎么办?

model Post:

#/models/post.rb 
class Post < ActiveRecord::Base
   has_many :comments
   validates :title, :content, :presence => true
end

model Comment:

#/models/comment.rb
class Comment < ActiveRecord::Base
   belongs_to :post
   validates :name, :comment, :presence => true
end

Comments 评论表单的控制器

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

视图:

/views/comments/_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>
  <% if @comment.errors.any?  %>
     error! 
  <% end %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :comment %><br />
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

/views/posts/show.html.erb

<%= render 'comments/form' %>

如何从控制器 CommentController 传递 @comment 到视图 /post/show.html .erb?

提前致谢。

I do a simple blog on rails.
I have a Post model and a Comment model.
When you create a comment, if comment is not valid, i want to show the error.
How do I do?

model Post:

#/models/post.rb 
class Post < ActiveRecord::Base
   has_many :comments
   validates :title, :content, :presence => true
end

model Comment:

#/models/comment.rb
class Comment < ActiveRecord::Base
   belongs_to :post
   validates :name, :comment, :presence => true
end

Comments Controller

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

View for comment form:

/views/comments/_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>
  <% if @comment.errors.any?  %>
     error! 
  <% end %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :comment %><br />
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

/views/posts/show.html.erb

<%= render 'comments/form' %>

How to pass @comment from the controller CommentController to view /post/show.html.erb ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2025-01-03 11:28:39

CommentsController 中放置 render“posts/show” 而不是 redirect_to post_path(@post)

Put render "posts/show" instead of redirect_to post_path(@post) in your CommentsController.

何处潇湘 2025-01-03 11:28:39

如果评论无效,您不应重定向到 post_path(@post)

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])

    if @comment.save
      redirect_to post_path(@post), notice: 'Comment was successfully created.'
    else
      render action: "posts/show", error: 'The comment you typed was invalid.'
    end
  end
end

并将 /views/comments/_form.html.erb 中的第一个表单行从: 更改为

<%= form_for([@post, @post.comments.build]) do |f| %>

<%= form_for([@post, (@comment || @post.comments.build)]) do |f| %>

然后在保存失败时您应该看到错误消息。

you shouldn't redirect to post_path(@post) if the comment is not valid.

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])

    if @comment.save
      redirect_to post_path(@post), notice: 'Comment was successfully created.'
    else
      render action: "posts/show", error: 'The comment you typed was invalid.'
    end
  end
end

and change the first form line in /views/comments/_form.html.erb from:

<%= form_for([@post, @post.comments.build]) do |f| %>

to:

<%= form_for([@post, (@comment || @post.comments.build)]) do |f| %>

then you should see error messages when it fails to save.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文