将变量从控制器传递到视图
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
CommentsController
中放置render“posts/show”
而不是redirect_to post_path(@post)
。Put
render "posts/show"
instead ofredirect_to post_path(@post)
in yourCommentsController
.和/或查看 Ryan Bates 有关嵌套模型和资源的截屏视频:
它们是 Rails 2,但了解它是如何工作的也没关系。
也许您也感兴趣:
And/Or take a look at Ryan Bates Screencasts about nested models and resources:
They're Rails 2 but to get an idea how it works it's ok.
Maybe also interesting for you:
如果评论无效,您不应重定向到
post_path(@post)
。并将
/views/comments/_form.html.erb
中的第一个表单行从: 更改为:
然后在保存失败时您应该看到错误消息。
you shouldn't redirect to
post_path(@post)
if the comment is not valid.and change the first form line in
/views/comments/_form.html.erb
from:to:
then you should see error messages when it fails to save.