-1

我想从客户端获取数据,并在创建方法中使用 post 方法显示在我的 ror 网站上,如何编写从客户端(android)获取数据到 ror 的函数。

def create    

    @post = Post.new(params[:post])
    data_json = JSON.parse request.body.read
    #respond_to do |format|
    if @post.save
      flash[:notice] = "Prayer Successfully created."
      @posts = Post.paginate(page: params[:page],:per_page => 5)
      @post = Post.new(data_json)
   @post.save
      #format.json{ render :json => @post, :status => :created  }
    else 
      flash[:notice] = "Error"
      @posts = Post.paginate(page: params[:page],:per_page => 5)

      #format.json{ render :json => @post, :status => :created  }
    end



  end
4

1 回答 1

0

There is not enough data in your question but ill try to help you.

Your client-side logic should look like that (jQuery code):

$("#some_button").bind("click", function(event){
    $.ajax({
        type: "POST",
        url: "/bla-bla/create",
        data: { my_param: JSON.stringify(my_data) }
    });
});

And action of controller that match to route /bla-bla/create:

def create
    my_var = JSON.parse(params[:my_param])
    ... 
    #do what you want with "my_var"

Hope it'll help you!

于 2013-05-27T11:23:39.750 回答