我想向我的Ruby on Rails本地服务器发送POST请求。不幸的是,起初它不会让我POST,因为protect_from_forgery,所以我最终在开发和生产中将它设置为false。基本上,我的问题是我将发送来自我的Cocoa应用程序的POST请求和所有想要的参数,Ruby服务器将会看到它。但是,每当服务器插入我的请求时,除created_at和updated_at之外,所有VALUES都设置为NULL。我不知道Ruby on Rails是如何工作的(因为我使用脚手架),所以我不确定需要设置哪些参数以便它们能够正确插入。Ruby on Rails和Cocoa - POST将值作为NULL放入数据库
谢谢!
可可CODE:
NSString *urlString = @"http://localhost:3000/clients";
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL: url];
[req setHTTPMethod: @"POST"];
NSData *putParams = [@"name=TEST&ip_address=1.1.1.1" dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPBody: putParams];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
NSLog(@"Response: %@", result);
[url release];
[req release];
[error release];
服务器响应:
Processing ClientsController#create (for 17.224.23.62 at 2010-07-13 16:59:55) [POST]
Parameters: {"name"=>"TEST", "ip_address"=>"1.1.1.1"}
Client Create (0.3ms) INSERT INTO "clients" ("name", "created_at", "updated_at", "ip_address") VALUES(NULL, '2010-07-13 23:59:55', '2010-07-13 23:59:55', NULL)
Redirected to http://17.224.23.62:3000/clients/6
Completed in 7ms (DB: 0) | 302 Found [http://17.224.23.62/clients]
Processing ClientsController#show (for 17.224.23.62 at 2010-07-13 16:59:55) [GET]
Parameters: {"id"=>"6"}
Client Load (0.1ms) SELECT * FROM "clients" WHERE ("clients"."id" = 6)
Rendering template within layouts/clients
Rendering clients/show
Completed in 7ms (View: 2, DB: 0) | 200 OK [http://17.224.23.62/clients/6]
我试图做这CLIENTS控制器,但问题仍然是相同的:
if params[:name].present?
@client = Client.new(:name => params[:names])
else
@client = Client.new(params[:client])
end
JUST IN情况下,需要(完整的客户端控制器 - 所有这些都与SCAFFOLD相关):
class ClientsController < ApplicationController
# GET /clients
# GET /clients.xml
def index
@clients = Client.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @clients }
end
end
# GET /clients/1
# GET /clients/1.xml
def show
@client = Client.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @client }
end
end
# GET /clients/new
# GET /clients/new.xml
def new
@client = Client.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @client }
end
end
# GET /clients/1/edit
def edit
@client = Client.find(params[:id])
end
# POST /clients
# POST /clients.xml
def create
if params[:name].present?
@client = Client.new(:name => params[:names])
else
@client = Client.new(params[:client])
end
respond_to do |format|
if @client.save
format.html { redirect_to(@client, :notice => 'Client was successfully created.') }
format.xml { render :xml => @client, :status => :created, :location => @client }
else
format.html { render :action => "new" }
format.xml { render :xml => @client.errors, :status => :unprocessable_entity }
end
end
end
# PUT /clients/1
# PUT /clients/1.xml
def update
@client = Client.find(params[:id])
respond_to do |format|
if @client.update_attributes(params[:client])
format.html { redirect_to(@client, :notice => 'Client was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @client.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.xml
def destroy
@client = Client.find(params[:id])
@client.destroy
respond_to do |format|
format.html { redirect_to(clients_url) }
format.xml { head :ok }
end
end
end
P.S.如果我的问题有些不清楚,请告诉我。
需要看到控制器和发布请求才能看到这些工作是如何工作的 - 所以请在更新时加入。 – 2010-07-14 00:15:48
你好杰米。我刚刚进行了更新。如果你能帮忙,我会很感激! – hassaanm 2010-07-14 00:26:43