Ini adalah tampilan form yang akan kita buat, mungkin , pertama buatlah controller serta view comment
/view/layouts/comments.html.erb
<!DOCTYPE html><html> <head> <title>TestRails</title>
<%= stylesheet_link_tag params[:controller] %> <%= javascript_include_tag "jquery-1.6.4.min.js"%> <%= javascript_include_tag "jquery-ui.min.js"%> <%= javascript_include_tag "jquery.livequery.js" %> <%= javascript_include_tag "jquery.timeago.js" %> <%= javascript_include_tag "jquery_ujs"%> <%= javascript_include_tag params[:controller]%> <%= csrf_meta_tags %> </head> <body>
<%= yield %>
</body></html>
/controller/comments_controller
class CommentsController < ApplicationController
respond_to :html, :js
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
@comment = Comment.new
respond_to do |format|
format.html # index.html.erb
format.rss
end
end
def auto
@comment = Comment.all
respond_to do |format|
format.html
format.json { render json: @comment}
end
end
# GET /comments/1
# GET /comments/1.json
def show
@comment = Comment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @comment }
end
end
# GET /comments/new
# GET /comments/new.json
def new
@comment = Comment.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
end
# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(params[:comment])
flash.discard[:notice] = "Comment successfully created" if @comment.save
respond_with( @comment, :layout => !request.xhr? )
end
# PUT /comments/1
# PUT /comments/1.json
def update
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.update_attributes(params[:comment])
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
respond_with( @comment, :layout => !request.xhr? )
end
end
var el = $('#new_comment');/view/comments/delete.js.erb
//ajax stop function
$("#comment-notice").ajaxStop(function(){
$(this).fadeOut();
});
//ajax start function
$("#comment-notice").ajaxStart(function(){
$(this).fadeIn();
});
<% if @comment.errors.any? %>
// Create a list of errors
var errors = $('<ul />');
<% @comment.errors.full_messages.each do |error| %>
errors.append('<li><%= escape_javascript( error ) %></li>');
<% end %>
// Display errors on form
el.find('.errors').html(errors);
<% else %>
//Insert a notice between the last comment and the comment form
//update comment count
$("#comments_count").html("<%= pluralize(Comment.count, 'Comment') %>");
// We could also render a partial to display the comment here
$('.comments').append("<div id='comment_<%= @comment.id %>' class='comment'>"+
"<div id='delete'><%= escape_javascript(link_to 'Destroy', @comment, method: :delete, :remote => true) %></div>"+
"<span id='dateandoptions' >Posted <abbr class='timeago' title='<%= @comment.created_at.to_time.iso8601 %>'></abbr>"+
" </span><%= escape_javascript( simple_format( @comment.comment )) %> <br></br>"+
"</div>");
/* Highlight the new comment */
// Clear form
el.find('input:text,textarea').val('');
el.find('.errors').empty();
<% end %>
$("#comment_<%= @comment.id %>").fadeOut();
//update comment count
$("#comments_count").html("<%= pluralize(Comment.count, 'Comment') %>");
/view/comments/index.html.erb
<span id="comments_count"><%= pluralize(@comments.count, "Comment") %></span>
<div class="comments">
<%= render :partial => @comments, :locals => { :list => true } %>
</div>
<hr />
<div id="comment-notice"></div>
<h2>Say something!</h2>
<div id="forms">
<%= render :partial => 'form' %>
</div>
/view/comments/_comment.html.erb
<%= div_for comment do %>
<div id="delete">
<%= link_to 'Destroy', comment, method: :delete , :remote => true%>
</div>
<span id="dateandoptions">
Posted <abbr class="timeago" title="<%= comment.created_at.to_time.iso8601 %>"></abbr><br />
</span>
<%= content_tag(:p, comment.comment, :class => "comment-body") %>
<% end %>
/view/comments/_form.html.erb
<%= form_for (@comment), :remote => true, :html => { :id => 'new_comment' } do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_field :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Gimana bingung ya sama script diatas heheh, ya udah deh biar gak bingung script full bisa di download di TESTRAILS

Tidak ada komentar:
Posting Komentar