skip to main
|
skip to sidebar
memo temp
2008年12月13日 星期六
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)
state chart
eb
關於我自己
Lake
檢視我的完整簡介
pie chart
網誌存檔
►
2010
(1)
►
11月
(1)
►
2009
(26)
►
1月
(26)
▼
2008
(26)
▼
12月
(26)
[HTML] IE 表單 Enter 的 Bug
ruby memo
rails default mode
deploy
Gem dependencies
liquid,dynamic content
All About Layouts
rails 2.2 extra, delegate,gem
each_with_object,inject
header, redirect_to , render js, polymorphic_path
Rescue from dispatching, layout mailer
Shallow Routes, Resources With Specific Actions
etag,last_modified,Get
rails, thread-safe
alias_method_chain
rails2.2 extra, dynamic finder
join,include
sql,DATE,group by
Block in views, concat, capture, content_for,yield...
memoizable,rails2.2
overwrite attr_writer,=
rails I18n sample
rails execute sql
jquery parse xml
google ajax library api
using jquery in rails,jquery, rails,form,reset,esc...
沒有留言:
張貼留言