Ruby on Rails 和 Cocoa - POST 将值作为 NULL 放入数据库

发布于 2024-09-09 04:41:18 字数 4248 浏览 2 评论 0原文

我正在尝试向我的 Ruby on Rails 本地服务器发送 POST 请求。不幸的是,一开始它不允许我POST,因为protect_from_forgery,所以我最终在开发和生产中将其设置为 false。基本上,我的问题是我将从 Cocoa 应用程序发送带有所有所需参数的 POST 请求,并且 Ruby 服务器将看到它。但是,每当服务器插入我的请求时,除了created_at 和updated_at 之外,所有值都设置为NULL。我不知道 Ruby on Rails 是如何工作的(因为我使用了脚手架),所以我不确定需要在哪里设置参数以便正确插入它们。

谢谢!

可可代码:

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]

我尝试在客户端控制器中执行此操作,但问题仍然相同:

if params[:name].present?
  @client = Client.new(:name => params[:names])
else
  @client = Client.new(params[:client])
end

以防万一需要(完整的客户端控制器 - 所有这些都带有支架):

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

PS 如果我的问题不清楚,请让我知道。

I am trying to send a POST request to my Ruby on Rails local server. Unfortunately, at first it wouldn't let me POST because of protect_from_forgery, so I ended up setting it to false in development and production. Basically, my issue is that I'll send the POST request from my Cocoa app with all of the wanted params, and the Ruby server will see it. However, whenever the server inserts my request all of the VALUES are set to NULL with the exception of created_at and updated_at. I do not know how Ruby on Rails works (since I used scaffold), so I am not sure where I need to set the params so that they will be inserted properly.

Thanks!

COCOA 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];

SERVER RESPONSE:

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]

I TRIED TO DO THIS IN CLIENTS CONTROLLER BUT ISSUE REMAINED THE SAME:

if params[:name].present?
  @client = Client.new(:name => params[:names])
else
  @client = Client.new(params[:client])
end

JUST IN CASE IT IS NEEDED (FULL CLIENT CONTROLLER - ALL OF IT WAS WITH 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. If something about my question is unclear, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-09-16 04:41:18

您的控制器中有一个拼写错误: @client = Client.new(:name => params[:names]) 应该是 @client = Client.new(:name =>;参数[:名称])

这可以解决部分问题,但问题的根源在于:

本质上,如果这是一个“真正的”Rails 应用程序,它会发送如下参数:

{ :client => { :name => x, :ip => y } }

然后您可以像这样访问 params[:client] 中的名称和 IP :

@client = Client.new(params[:client])

但是你正在发送:

{ :name => x, :ip => y }

所以你需要:

@client = Client.new(:name => params[:name], :ip => params[:ip])

为了让“Rails方式”工作,你可以修改你的Objective-C代码来发送像client[name]=x&client[ip]=y

You have a typo in your controller: @client = Client.new(:name => params[:names]) should be @client = Client.new(:name => params[:name]).

That would solve part of the issue but here is the root of it:

Essentially, if this were a "real" Rails app it would send params like this:

{ :client => { :name => x, :ip => y } }

And then you could access the name and IP in params[:client] like so:

@client = Client.new(params[:client])

But you are sending:

{ :name => x, :ip => y }

So you need:

@client = Client.new(:name => params[:name], :ip => params[:ip])

For the "Rails way" to work, you could modify your Objective-C code to send params like client[name]=x&client[ip]=y.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文