我已经参加了Michael Hartl的Rails 3 Tutorial应用程序,并在几个领域对其进行了扩展。但是,我保持登录和会话处理相同。我想与iPhone应用程序接口,但我不知道如何。我看过RestKit和Objective Resource,但认为我会推出自己的产品。我一直在用cURL进行测试,但迄今为止没有运气。我一直在使用这个命令Rails 3教程cURL/iOS登录
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://www.example.com/signin -d "{'session' : { 'email' : '[email protected]', 'password' : 'pwd'}}" -c cookie
正如在3教程中,我使用的会话Rails的。
这些路线:
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
这是控制器:
class SessionsController < ApplicationController
def new
@title = "Sign in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end
def destroy
sign_out
redirect_to root_path
end
end
没有模型和你的表单登录。这里是表格的HTML:
<h1>Sign In</h1>
<%= form_for(:session, :url => sessions_path) do |f| %>
<div class="field">
<%= f.label :email %></br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %></br>
<%= f.password_field :password %>
</div>
<div class="actions">
<%= f.submit "Sign in" %>
</div>
<% end %>
<p> New user? <%= link_to "Sign up now!", signup_path %></p>
对不起,如果这是太多的信息,我想尽可能地给。
基本上,我想能够从本地iPhone应用程序访问我的Rails数据库。如果有人有关于如何登录,存储会话,然后拨打其他网站的建议,我将不胜感激。
但是,如果这是不可能的,一个工作cURL请求可能会让我朝着正确的方向前进。谢谢!
这可能是一个愚蠢的问题,但我还是比较新的Rails。 format.json到底做了什么?这是否允许我将参数作为JSON发送?如果我现在format.js,将其改为format.json拧什么了? – NSchulze 2012-08-01 18:27:15
是的,它发送参数为JSON。 Json是JavaScript的一个子集,尽管不是所有的JavaScript响应都是json响应。 Json可以被JavaScript读取,并且它是一种将数据从服务器传递到JavaScript的快速而有效的方法。 – JohnMerlino 2012-08-01 18:31:13
很酷。那么是我的cURL命令不工作的原因,因为它是format.js?还是我在那里的基地? – NSchulze 2012-08-01 21:14:31