嗯,我是Ruby on Rails的新手,我正在尝试创建一个包含群组的网站,每个群组都包含帖子,每个帖子都有评论。我已经编写了以下代码并且它正在工作,但现在我正在尝试使用partials编写此代码,然后在组显示视图中呈现这些部分,而不是在组显示视图中编写帖子和注释的整个代码。我一直在努力做到这一点,但很多错误显而易见,显然我做错了,任何人都可以帮助我如何使用partials编写我的代码?
这是我目前的代码:
评论控制器:
class CommentsController < ApplicationController
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.new
end
def create
@post = Post.find(params[:post_id])
@group = Group.find(@post.group_id)
@startup = Startup.find(session[:entity_ID])
@comment = @post.comments.new(comment_params)
@comment.commenter = @startup.name
@comment.startup_id = session[:entity_ID]
if @comment.save
redirect_to group_path(@group)
end
end
def show
@comment = Comment.find(params[:id])
end
def edit
@comment = Comment.find(params[:id])
end
def update
@comment = Comment.find(params[:id])
if @comment.update(comment_params)
redirect_to @comment
else
render 'edit'
end
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to comment_path
end
private
def comment_params
params.require(:comment).permit(:comment)
end
end
组显示视图:
<p>
<strong>Name:</strong>
<%= @group.name %>
</p>
<p>
<strong>Description:</strong>
<%= @group.description %>
</p>
<p>
<strong>Interest:</strong>
<%= @group.interest %>
</p>
<h2>Posts</h2>
<% @group.posts.each do |post| %>
<%= @post = post %>
<p>
<strong>Title:</strong>
<%= post.title %>
</p>
<p>
</p>
<p>
<strong>Text:</strong>
<%= post.text %>
</p>
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.commenter %></strong>
<%= ":" %>
<%= comment.comment %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([@group,@post, @post.comments.build]) do |f| %>
<p>
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<% end %>
<h2>Add a post:</h2>
<%= form_for([@group, @group.posts.build]) do |f| %>
<p>
<%= f.label :Title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :Text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_group_path(@group) %> |
<%= link_to 'Back', groups_path %>
routes.rb中:
SprintThrough::Application.routes.draw do
resources :groups do
resources :posts, concerns: :likable do
resources :comments, concerns: :likable
end
end
end
现在我想为帖子和评论创建部分内容,并在组显示视图中呈现部分内容。有什么帮助吗?
答案 0 :(得分:0)
我会使用以下代码制作_form_comment.html.erb
和_form_post.html.erb
个文件
#_form_comment.html.erb
<h2>Add a comment:</h2>
<%= form_for([@group,@post, @post.comments.build]) do |f| %>
<p>
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<% end %>
#_form_post.html.erb
<h2>Add a post:</h2>
<%= form_for([@group, @group.posts.build]) do |f| %>
<p>
<%= f.label :Title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :Text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
并在groups show page
中调用这些部分,如下所示
<p>
<strong>Name:</strong>
<%= @group.name %>
</p>
<p>
<strong>Description:</strong>
<%= @group.description %>
</p>
<p>
<strong>Interest:</strong>
<%= @group.interest %>
</p>
<h2>Posts</h2>
<% @group.posts.each do |post| %>
<%= @post = post %>
<p>
<strong>Title:</strong>
<%= post.title %>
</p>
<p>
</p>
<p>
<strong>Text:</strong>
<%= post.text %>
</p>
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.commenter %></strong>
<%= ":" %>
<%= comment.comment %>
</p>
<% end %>
<%= render :partial => 'form_post' %>
<%= render :partial => 'form_comment' %>
<%= link_to 'Edit', edit_group_path(@group) %> | <%= link_to 'Back', groups_path %>
注意:这些partial
文件应位于groups views
下以使其正常工作。