-1

I want to get data from client side,and display in my ror website using post method in create method,how to write the function for fetching data from client side(android) to 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
Arihant Godha
  • 2,339
  • 2
  • 26
  • 50
Jitendra Patidar
  • 340
  • 2
  • 6
  • 17

1 Answers1

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!

Parandroid
  • 513
  • 2
  • 11
  • SO you need to know the format of incoming data. But the main principle is that you should parse incoming parameter which contains string representation of your json. – Parandroid May 27 '13 at 11:27
  • i felt that your def creat code is fine,but i need some clarity,i am parsing from my client side "content", – Jitendra Patidar May 27 '13 at 11:29
  • could you clearly mention what i want to do in post method in def create function – Jitendra Patidar May 27 '13 at 11:34
  • http://pastebin.com/e0axwZiC see this link,what are the changes i want to do in def create function for post method – Jitendra Patidar May 27 '13 at 11:36
  • If your Post is Rails resource ("resources :posts" in routes.rb) then your create action already matches POST method. Maybe you want allow 2 types of creating posts - one from your app and one from json, coming from android app. Is it close to reality? – Parandroid May 27 '13 at 12:00
  • ya dude,now problem is how to write post method of fetching data from client side thats enought,i felt your second code is somewhat fine,but need to do more in that code, http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails see this link,and tellme how to do in my code – Jitendra Patidar May 27 '13 at 12:35
  • can you edit your post and add example of request that comes from android app? – Parandroid May 27 '13 at 15:27
  • no dude,in ror only we have to do some changes in post method@Parandroid – Jitendra Patidar May 28 '13 at 04:57
  • whats ur gtalk id,when can talk,i didnt get the solution@Parandroid – Jitendra Patidar May 28 '13 at 05:08
  • where i have to write the first code,is this somemore coded is needed – Jitendra Patidar May 28 '13 at 06:09
  • are you there,which code is correct i changed somewhat eventhough is not working – Jitendra Patidar May 28 '13 at 09:35