JavaScriptHelperのlink_to_remote

5074 ワード

R 1.RORエンジニアリングの作成
具体生成手順略
 
R 2.controllerコントローラクラスLinkAjaxTagControlを作成し、対応するテンプレートindex.rhtmlとajax_content.rhtml
コントローラコントローラクラスLinkAjaxTagController対応ファイルはlink_ajax_tag_ controller.rbはapp/controllersディレクトリの下にあります.
index.rhtmlファイルとajax_content.rhtmlはapp/views/link_にありますajax_tagディレクトリの下.
 
R 3.コントローラクラスの編集
1    class LinkAjaxTagController < ApplicationController
2        before_filter :set_charset
3  
4        def set_charset
5            @headers["Content-Type"] = "text/html; charset=GB2312"
6        end
7  
8        def index
9            render(:template => "link_ajax_tag/index")
10      end
11 
12      def get_page
13          render(:partial => "link_ajax_tag/ajax_content")
14      end
15   end
2~6行目:ここでは、出力テンプレートファイルの文字セットを設定するフィルタであり、この例ではGB 2312に設定する.
8~10行目:テンプレートファイルを指定します.このコードはテンプレートファイルを明示的に指定し、ここでは明示的に指定せずにRoRによって自動的に検索することもできます.
12~14行目:ローカルテンプレートajax_を取得content.rhtmlファイル.
 
Example:
# Generates: <a href="#" onclick="new Ajax.Updater('posts', '/blog/destroy/3', {asynchronous:true, evalScripts:true});
# return false;">Delete this post</a>
link_to_remote "Delete this post", :update => "posts",
:url => { :action => "destroy", :id => post.id }

# Generates: <a href="#" onclick="new Ajax.Updater('emails', '/mail/list_emails', {asynchronous:true, evalScripts:true});
# return false;"><img alt="Refresh" src="/images/refresh.png?" /></a>
link_to_remote(image_tag("refresh"), :update => "emails",
:url => { :action => "list_emails" })# )

You can override the generated HTML options by specifying a hash in options[:html] .
link_to_remote "Delete this post", :update => "posts", :url => post_url(@post), :method => :delete,
                         :html => { :class => "destructive"}
# Generates: <a href="#" onclick="new Ajax.Updater({success:'posts',failure:'error'}, '/blog/destroy/5',
# {asynchronous:true, evalScripts:true}); return false;">Delete this post</a>
link_to_remote "Delete this post",
:url => { :action => "destroy", :id => post.id },
:update => { :success => "posts", :failure => "error" }





 
 




 
R 4.ajax_を編集するcontent.rhtmlファイル
1   

<%= Time.now %>


1行目:現在の時刻(秒単位)を取得します.
 
R 5.index.rhtmlファイルの編集
1   
2      
3        link_to_remoteテスト
4        <%= javascript_include_tag "prototype"%>
5     
6      
7        


8          <%= link_to_remote(「ページを取得(重複しない)」
9                                :update=>「page_space_1」
10                                 :url => {:action => :get_page }) %>
11         


12       


13       


14         <%= link_to_remote(「ページを取得(繰り返し)」、
15                                 :update => "page_space_2 ",
16                                 :url => {:action => :get_page },
17                                 :position => "after") %>
18         


19       


20     
21  
4行目:javascript_を使用include_tagラベルにはprototype.jsファイルが含まれます.
8~10行目:link_を使用to_remoteタグは、Ajax機能をサポートするリンクを生成します.updateパラメータ項目は、更新領域を関連付けるid値(この例では11行目のコードで定義された更新領域を関連付ける):urlパラメータ項目は、要求する動作方法を指定します.このセグメントコードによって生成されるHTMLコードは以下の通りである.
取得页面(重复)
 
R 6.テスト
WEBrickサーバーを起動し、IEアドレスバーに入力するhttp://127.0.0.1:3000/link_ajax_tag