ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2008年12月14日 星期日
using jquery in rails,jquery, rails,form,reset,escape,fn,plugin
jquery railscast
The jSkinny on jQuery
Myth #3: Rails forces you to use Prototype
jquery post
$.post(url, data, callback, type)
This is an easy way to send a simple POST request to a server without having to use the
more complex $.ajax function. It allows a single callback function to be specified that will
be executed when the request is complete (and only if the response has a successful
response code). The returned data format can be specified by the fourth paramater. If you
need to have both error and success callbacks, you may want to use $.ajax. $.post is a
(simplified) wrapper function for $.ajax.
//那要怎樣告訴controller 我們的format 是js勒 有兩種方法, 方法一
$(document).ready(function(){
$("#new_forum_comment form").submit(function(){
$.post($(this).attr("action")+'.js', $(this).serialize(), null, "script");
return false;
})
})
ajaxSetup
//第二種就是在發出ajax 前 把header 設一設
//ajaxSetup: Setup global settings for AJAX requests.
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
$(document).ready(function(){
$("#new_forum_comment form").submit(function(){
$.post($(this).attr("action"), $(this).serialize(), null, "script");
return false;
})
})
#comment controller
def create
@forum = params[:model].constantize.find(params[:id])
params[:comments][:user_id] = @me.id
if @forum.comments.create(params[:comments]) && @forum.is_a?(Forum)
@forum.last_comment_time = @forum.comments.last.created_at
@forum.save
end
@comment = @forum.comments.last
respond_to do |format|
format.html {redirect_to :back}
format.js
end
end
escape_javascript
form reset
#create.js.erb
#rjs 是 prototype在用的, 這邊是jquery 所以不能用
#記得把 form reset 清空
$("#comments_list").append("<%= escape_javascript(render :partial => 'shared/comments', :locals => {:comment =>@comment })%>");
$("#new_forum_comment form")[0].reset();
jquery fn
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
};
$(document).ready(function(){
$("#new_forum_comment form").submitWithAjax();
})
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言