Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <h1>
- <%= @post.title %>
- </h1>
- <p>
- <%= @post.body %>
- </p>
- <h1 class="comments">Comments</h1><br />
- <!-- #1 -->
- <% unless @post.comments.empty? %>
- <% @post.comments.each do |comment| %>
- <p><%= h comment.body %><br /><small>Posted on <%= h comment.created_at %> by <%= comment.user_id.login %></small>
- </p>
- <% end %>
- <% end %>
- <!-- #2 -->
- <h2 class="comments">Add a new Comment</h2><br />
- <% if logged_in? %>
- <%= render :partial => @comment = Comment.new,
- :locals => { :button_name => 'Create'}%>
- <% else %>
- <p> Please <%= link_to "Login", login_path %> or <%= link_to "Sign Up", signup_path %> to enter a comment. Thank you</p>
- <% end %>
- <%= link_to 'Back', posts_path %>
- <br /><%= debug(session) %>
- ----
- controller
- class CommentsController < ApplicationController
- before_filter :load_post
- # GET /comments
- # GET /comments.xml
- def index
- @comments = @post.comments.find(:all)
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @comments }
- end
- end
- # GET /comments/1
- # GET /comments/1.xml
- def show
- @comment = @post.comments.find(params[:id])
- @comment.user_id = @current_user
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @comment }
- end
- end
- # GET /comments/new
- # GET /comments/new.xml
- def new
- @comment = @post.comments.build
- @post.comment.user_id = @current_user
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @comment }
- end
- end
- # GET /comments/1/edit
- def edit
- @comment = @post.comments.find(params[:id])
- end
- # POST /comments
- # POST /comments.xml
- def create
- @comment = @post.comments.build(params[:comment])
- @comment.user_id = @current_user
- respond_to do |format|
- if @comment.save
- flash[:notice] = 'Comment was successfully created.'
- format.html { redirect_to(@post) }
- format.xml { render :xml => @comment, :status => :created, :location => @comment }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
- end
- end
- end
- # PUT /comments/1
- # PUT /comments/1.xml
- def update
- @comment = @post.comments.find(params[:id])
- respond_to do |format|
- if @comment.update_attributes(params[:comment])
- flash[:notice] = 'Comment was successfully updated.'
- format.html { redirect_to(@post) }
- format.xml { head :ok }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
- end
- end
- end
- # DELETE /comments/1
- # DELETE /comments/1.xml
- def destroy
- @comment = @post.comments.find(params[:id])
- @comment.destroy
- respond_to do |format|
- format.html { redirect_to(post_comments_url) }
- format.xml { head :ok }
- end
- end
- def load_post
- @post = Post.find(params[:post_id])
- end
- end
Add Comment
Please, Sign In to add comment