0
嗯,我是Ruby on Rails的新手,我尝试创建一个拥有组的网站,每个组包含帖子,每个帖子都有注释。我已经写了下面的代码,它正在工作,但现在我正在尝试使用partials编写此代码,然后在groups show view中呈现这些partials,而不是在group show view中编写帖子和评论的整个代码。我一直在努力做到这一点,但很多错误显然很明显我一直在做错,任何人都可以帮助我如何使用partials编写我的代码?在rails中使用partials和渲染4
这里是目前我的代码:
评论控制器:
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
现在我想为帖子创建部分a发表评论并在分组中显示视图。请帮忙吗?
如果我需要将这些偏好置于评论或帖子下并将它们分组,则该怎么做? – user3166918
@ user3166918你可以这样做:<%= render:partial =>'posts/form_post'%>'或'<%= render:partial =>'comments/form_post'%> – Pavan