ruby-on-rails - 如何在没有 Rhosync 或 RhoConnect 的情况下使用 Rhodes?

标签 ruby-on-rails ruby jquery-mobile rhodes

我知道我们可以使用直接网络服务在没有 Rhosync 或 Rhoconnect 的情况下使用 rhodes 同步数据,但我在这里有点混淆将代码放置在何处以进行网络服务调用以及我们如何初始化它。谁能帮我举个小例子?

提前致谢。

最佳答案

我明白了,它对我有用。

class ProductController < Rho::RhoController
  include BrowserHelper

    # GET /product
  def index
    response =  Rho::AsyncHttp.get(:url => "example.com/products.json",
     :headers => {"Content-Type" => "application/json"})
     @result = response["body"] 

    render :back => '/app'
  end

    # GET /product/{1}
  def show
    id =@params['id']
    response =  Rho::AsyncHttp.get(:url => "example.com/products/"+ id +".json",
    :headers => {"Content-Type" => "application/json"})

    @result = response["body"]
  end

   # GET /product/new
  def new
    @product = product.new

    render :action => :new, :back => url_for(:action => :index)
  end

    # GET /product/{1}/edit
  def edit
    id =@params['product_id'].to_s
    response =  Rho::AsyncHttp.get(:url => "example.com/products/#{id}.json",
    :headers => {"Content-Type" => "application/json"})

    @result = response["body"]
  end

    # POST /product/create
  def create
    name = @params['product']['name']
    price = @params['product']['price']
    body = '{"product" : {"name" : "'+ name +'","price" :"'+ price +'"  } }'

    @result =  Rho::AsyncHttp.post(:url => "example.com/products.json",
    :body => body, :http_command => "POST", :headers => {"Content-Type" => "application/json"})  

    redirect :action => :index
  end

    # POST /product/{1}/update
  def update

    name=@params['product']['name']
    price=@params['product']['price']

    body = '{"product" : {"name" : "' + name + '","price" :"' + price + '"  } }'
    id = @params["product_id"].to_s

    response =  Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json",
    :body => body, :http_command => "PUT",:headers => {"Content-Type" => "application/json"})

    redirect :action => :index
  end

    # POST /product/{1}/delete
  def delete
    id = @params["product_id"].to_s
    response =  Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json",
    :http_command => "DELETE", 
    :headers => {"Content-Type" => "application/json"})  

    redirect :action => :index  
  end
end

关于ruby-on-rails - 如何在没有 Rhosync 或 RhoConnect 的情况下使用 Rhodes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11095213/

相关文章:

jquery - jquery mobile 中的导航栏图标问题

ruby-on-rails - 从模型中注销设计用户

ruby-on-rails - 在一页上创建目标和任务的 Rails 待办事项列表应用程序 - 缺少参数

ruby-on-rails - 将 Add 方法添加到 ActiveRecord 数组

ruby - 如何在终端中创建交互式菜单?

ruby-on-rails - postgres 中的小数列。有没有一种方法可以存储没有尾随零的值

jQueryMobile : how to work with slider events?

JQuery 移动应用 - 表单安全问题

ruby-on-rails - Rails路由约束和UTF-8

ruby - 如何将文本文件或字符串传递给 Ruby 脚本并在命令行中输出结果?