2014-05-10 79 views
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發表評論並在分組中顯示視圖。請幫忙嗎?

回答

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使這項工作。

+0

如果我需要將這些偏好置於評論或帖子下並將它們分組,則該怎麼做? – user3166918

+0

@ user3166918你可以這樣做:<%= render:partial =>'posts/form_post'%>'或'<%= render:partial =>'comments/form_post'%> – Pavan