我正在尝试制作一个包含用户、帖子和评论的博客。每个用户可以有很多帖子和很多评论,类似地,每个帖子可以有很多评论。我已经成功地制作了用户和帖子部分,但在创建评论然后显示它们时遇到了困难。
代码:
路线.rb :
resources :users do
resources :posts do
resources :comments
end
end
用户.rb :
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
post.rb :
belongs_to :user
has_many :comments, dependent: :destroy
评论.rb:
belongs_to :post, :user
我正在帖子的 View 中创建和显示评论,所以..
post_controller.rb :
def show
@user = current_user
@post = Post.find(params[:id])
end
View /帖子/show.html.erb :
<p><strong>Title:</strong><%= @post.title %></p>
<p><strong>Text:</strong><%= @post.text %></p>
<% if @user.posts.comments.empty? %>
<h2>Comments</h2>
<%= render @posts.comments %>
<% end %>
<h2>Add a comment:</h2>
<%= render "comments/form" %>
<%= link_to 'Edit Post', edit_user_post_path(@user.id,@post) %> |
<%= link_to 'Back to Posts', user_posts_path(@user.id) %>
comment_controller.rb :
class CommentsController < ApplicationController
def create
@user = current_user
@post = @user.posts.find(params[:post_id])
@comment = @user.posts.comments.create(params[:comment])
redirect_to user_post_path(@user.id,@post)
end
def destroy
@user = current_user
@post = @user.posts.find(params[:post_id])
@comment = @user.posts.comments.find(params[:id])
@comment.destroy
redirect_to user_post_path(@user.id,@post)
end
end
部分是:
意见/评论/_form.html.erb :
<%= form_for([@user,@post,@comment]) do |f| %>
<p>
<%= @user.email %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我认为我的 form_for 不在这里,但我是 Rails 的新手,我也尝试过 form_for(@user,@post,@post.comments.build) 但这也不起作用......无论如何,这是另一个部分:
意见/评论/_comment.html.erb:
<p><strong>Commenter:</strong><%= @user.email %></p>
<p><strong>Comment:</strong><%= comment.body %></p>
<p><%= link_to 'Destroy Comment', [comment.post, comment],method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
再次在这里,我无法链接到...任何建议都会很棒。
最佳答案
您想创建一个包含用户、帖子和评论的博客,我看到您所做的与我之前创建博客时所做的有些不同。我会告诉你我做了什么(通过编辑你在你的问题中发布的文件的代码)然后尝试它是否适合你:)
1- routes.rb 让它像这样
resources :users
resources :posts do
resources :comments
end
2- user.rb 很好,不需要修改
3- post.rb 也很好
4-评论.rb
belongs_to :post
belongs_to :user
5-posts_controller.rb
def show
@post = Post.find(params[:id])
@comment = Comment.new
end
6- view/posts/show.html.erb(这个 View 应该让你能够看到帖子和评论以及一个新评论框,一个编辑帖子的链接和一个帖子索引的链接)
<p><strong>Title:</strong><%= @post.title %></p>
<p><strong>Text:</strong><%= @post.text %></p>
<h2>Comments</h2>
<%= render @posts.comments %>
<h2>Add a comment:</h2>
<%= render "comments/form" %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
7- comments_controller.rb(不要忘记再次添加destroy方法)
class CommentsController < ApplicationController
before_filter :load_post
def create
@comment = @post.comments.build(params[:comment])
@comment.user_id = current_user.id
if @comment.save
redirect_to @post, notice: "Added comment."
else
render :new
end
end
private
def load_post
@post = Post.find(params[:article_id])
end
end
8- views/comments/_form.html.erb (尝试以简单的方式先制作)
<%= form_for([@post,@comment]) do |f| %>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
9- 意见/评论/_comment.html.erb
<p><strong>Commenter:</strong><%= comment.user.email %></p>
<p><strong>Comment:</strong><%= comment.body %></p>
<p><%= link_to 'Destroy Comment', [comment.post, comment],method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
我希望这对你有用,因为它对我有用,尝试一下,让我知道它对你的影响,我的博客工作来自 the before code for revised episode 229 .
关于ruby-on-rails - 为嵌套路由创建注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288632/