2017-04-27 6 views
0

Rails 5 APIをPhonegapに接続する方法を詳細に説明するチュートリアルをどのように説明するのかを教えてください。私はRailsには比較的新しいので、phonegapに関する経験がなく、これについて数々のことを詳しく説明しています。私はフロントエンドにHTML5、CSS、JQueryを使用しています。Rails 5 APIとPhonegap

本当に助けていただきありがとうございます。

<?xml version='1.0' encoding='utf-8'?> 
<widget id="com.yourname.workshop" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <name>Workshop</name> 
    <description> 
     A sample Apache Cordova application that responds to the deviceready event. 
    </description> 
    <author email="[email protected]" href="http://cordova.io"> 
     Apache Cordova Team 
    </author> 
    <content src="http://localhost:3001" /> 
    <plugin name="cordova-plugin-whitelist" spec="1" /> 
    <access origin="*" /> 
    <allow-intent href="http://*/*" /> 
    <allow-intent href="https://*/*" /> 
    <allow-intent href="tel:*" /> 
    <allow-intent href="sms:*" /> 
    <allow-intent href="mailto:*" /> 
    <allow-intent href="geo:*" /> 
    <platform name="android"> 
     <allow-intent href="market:*" /> 
    </platform> 
    <platform name="ios"> 
     <allow-intent href="itms:*" /> 
     <allow-intent href="itms-apps:*" /> 
    </platform> 
</widget> 

答えて

2

Phonegapに書き込むフロントエンドアプリケーションをバックエンドのRails APIと「接続する」方法は、HTTP要求です。

Rails has an official guide for writing API-only applications.あなたのアプリはAPIだけを提供する必要はありませんが、簡単に解析できるデータを提供する必要があります。 (通常はJSON)

次に、フロントエンドでライブラリを使用して、バックエンドAPIによって定義された特定のエンドポイントにリクエストを行います。次に、レスポンスを解析して、必要な処理を実行できます。 jQuery makes it easy to make requests.

Railsでは、通常のCRUD操作がいくつかのブログや投稿の投稿に許可されているコントローラがあるとしましょう。それは次のようになります。

class PostsController < ApplicationController 
    responds_to :json 

    def show 
    @post = Post.find(params[:id]) 
    respond_with(@post) 
    end 

    def index 
    @posts = Post.all 
    respond_with(@posts) 
    end 

    def create 
    @post = Post.create(params[:post]) 
    respond_with(@post) 
    end 

    def update 
    @post = Post.find(params[:id]) 
    @post.update_attributes(params[:post]) 
    respond_with(@post) 
    end 
end 

今、あなたは(そのことについて、または何か他のもの)JavaScriptからこれらのアクションにHTTPリクエストを行うことができます。

$.get('/posts', {}, function(response){ 
    // response here is the data returned by the Post#index action 
}) 

$.post('/posts', {post: {content: "post content"}}, function(response){ 
    // response here is the data returned by the Post#create action 
}) 

これはかなり基本的な例であるが、ほとんどのWebアプリケーションはこのコンセプトの単なる変形です。

+0

ありがとうございました!特に助けになった例。そのすべてを理解することは愚かな質問かもしれませんが、私が開発中のときにコントローラ/サーバを電話帳に接続するにはどうすればよいですか?上記の編集でconfig.xmlファイルを挿入しました。私が追加した 'localhost'行はそのトリックを行いますか? – dgreen22

+0

私は自分でphonegapを使用したことはないので、私は確かに言うことはできません。しかし、それがすべての要求のためのドメインを設定すれば、それはうまくいくはずです – Brennan

関連する問題