問題描述
在我的域服務器上使用 Ruby on Rails 應用程序 (Consuming a Ruby on Rails app on my domain server)
I am running rails version 2.3.2 on my website domain and I am having a huge problem with wrapping my head around how this works:
I have my website running a RoR app on my domain development server. It is just a sample scaffold that allows you to type in your name, zip, state, etc. I am using ruby 1.8.2, and have a mysql server also.
I want to consume this data into my windows phone 7 through SOAP (I don't know if I even have one to begin with), but here is where I have problems.
In using visual studio, it cannot locate my server when i direct it over to my url. It gives the error saying that there was nothing in the correct format.
Maybe I don't have a server running? I want to have the data be parsed out into XML for the phone to consume, but I have no idea how to set this up!
Basically, I have the domain, and the phone, but no knowledge of the steps inbetween.
Can anyone help me get this up and running?
‑‑‑‑‑
參考解法
方法 1:
The easiest way to go about this is to simply emit JSON and consume that on your mobile application. Generally you just need to call .to_json
on an object and you're half done. SOAP requires a ton of XML overhead that's usually not worth it unless you're already neck‑deep in an enterprise application that's overflowing with it.
Updating your Rails stack to 2.3.11 and Ruby 1.8.7 is strongly encouraged as older versions of Rails, as with any application, have vulnerabilities. Ruby 1.8.2 is from around 2005 and is effectively ancient.
方法 2:
A few things to try:
First ‑ have you actually started the server? eg by running "script/server" ? You can test that the server is up and running by using "curl" (google for it to install/download" which is a very simple (and very commonly used) application for testing this stuff easily.
if you run curl and type in the url that you'd be accessing via your windows phone... and it responds with something (probably html), then the server is up and running. You can later use CURL to test if it responds to an xml request too.
Second: go look in the controller. See if it has a section such as:
respond_to do |format|
format.html
format.xml { render :xml => @widgets.to_xml }
end
it's the "respond_to" and "xml" bits that matter if you are going to get your system to consume xml. They should be present in every action in your controller. If not ‑ you will have to go a research how to do this for your code ‑ alternatively, using a later version of rails will let you use the up‑to‑date scaffold generators that should include these as standard.
Third: it is possible that your Windows phone app is just not requesting the resources in xml format and so Rails is returning html (which your SOAP parser won't understand). I don't know how you can check this, but what rails requires is for the HTML‑header: "Accept" to be set to "application/xml" or "text/xml"
You can also test this for any given URL with curl by using eg: "curl ‑H 'Accept: text/xml' 127.0.0.1/myapp" ‑ if it continues to spit out html (and not xml) then obviously it's not producing xml for that URL.
(by Richard、tadman、Taryn East)