2017-04-27 1 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.

레일즈에서는 블로그 나 다른 게시물의 일반적인 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 

지금 당신이 (그 문제에 대한 또는 다른 것) 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 
}) 

을하지만, 대부분의 웹 애플리케이션은이 개념의 변형 일뿐입니다.

+0

감사합니다. 특히 도움이되는 예. 그래서 모든 것을 이해하는 것은 어리석은 질문 일지 모르지만 개발 중에 컨트롤러/서버를 전화 갭에 어떻게 연결합니까? 위의 편집에서 config.xml 파일을 삽입했습니다. 내가 추가 한'localhost' 행은 그 트릭을 수행합니까? – dgreen22

+0

필자는 phonegap을 본 적이 없기 때문에 확실하게 말할 수는 없습니다. 그러나 이것이 모든 요청에 ​​대한 도메인을 설정하면 작동해야합니다. – Brennan