Reputation: 10759
So I have been trying out several different ways to connect my iPhone app to my ROR restful backend and all seems to work ok pulling down data (json) with 'get' requests, but when posting I am unable to do so. I have tried ObjectiveResource and two or three others and they all have the same issue. I am thinking its something set wrong in my ROR app? I did notice all the example iPhone projects use https for production apps, does a production app need to https for an iPhone to establish a session and be able to post?
HERE is what I get for an error if I use http://localhost:3000/posts
Processing PostsController#create (for 127.0.0.1 at 2011-04-05 20:49:42) [POST] Parameters: {"post"=>{"budget"=>"222"}} User interests hash: false
NoMethodError (undefined method posts' for false:FalseClass):
app/controllers/posts_controller.rb:113:in
create'
If I use http://localhost:3000
Processing PostsController#index (for 127.0.0.1 at 2011-04-05 20:49:42) [POST] Parameters: {"post"=>{"budget"=>"222"}}
Here is my Create method: def create #@post = Post.new(params[:post]) @post = current_user.posts.build(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
format.xml { render :xml => @post, :status => :created, :location => @post }
format.json { render :json => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
Here is the method:
# POST /posts
# POST /posts.xml
def create
#@post = Post.new(params[:post])
@post = current_user.posts.build(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
format.xml { render :xml => @post, :status => :created, :location => @post }
format.json { render :json => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
Iphone call:
// CRUD methods using Resource.h
- (void)createRemote {
NSString *url =
[NSString stringWithFormat:@"%@/posts", siteURL];
[Resource post:[self params] to:url];
}
%@/posts is equal to http://localhost:3000/posts
Upvotes: 0
Views: 521
Reputation: 96
As peterjb said, check for forgery protection. If you're on rails 3 then look for the csrf_meta_tag
in your application.html.erb.
Upvotes: 1
Reputation: 3189
Do you have protect_from_forgery
set on your ApplicationController?
This essentially kills POSTing information from anywhere except the forms on your website. Try commenting it out and see if things start working.
Upvotes: 1
Reputation: 2515
https -- is not must, it is how your server configures for the security needs. You can have http and https both no issues.
I suggest you to have look at the log in ROR app end too know what you get HTTP POST data -- if you are not able to get any data in ROR app end post some code which does POST request from your iPhone, people can analyse it better.
Upvotes: 0