这篇文章主要围绕Java:通过HTTPPost在RubyonRails应用程序中创建新的“产品”和java通过http调用接口展开,旨在为您提供一份详细的参考资料。我们将全面介绍Java:通过HTTP
这篇文章主要围绕Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”和java通过http调用接口展开,旨在为您提供一份详细的参考资料。我们将全面介绍Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”的优缺点,解答java通过http调用接口的相关问题,同时也会为您带来JRuby on Rails:在Rails应用程序中使用自定义Java类、ruby – 在rails 3中创建新应用程序时运行bundle install、ruby-on-rails – Postgres不会使用c9.io连接到Ruby on Rails应用程序中的服务器、ruby-on-rails – RACK_ENV在Rails应用程序中做了什么?的实用方法。
本文目录一览:- Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”(java通过http调用接口)
- JRuby on Rails:在Rails应用程序中使用自定义Java类
- ruby – 在rails 3中创建新应用程序时运行bundle install
- ruby-on-rails – Postgres不会使用c9.io连接到Ruby on Rails应用程序中的服务器
- ruby-on-rails – RACK_ENV在Rails应用程序中做了什么?
Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”(java通过http调用接口)
在Android上使用Apache HttpClient,如何使用HttpPost将数据发送到RESTfull Ruby on Rails应用程序。
这是我的控制器:
# POST /products def create @product = Product.new(params[:product]) respond_to do |format| if @product.save flash[:notice] = ''Product was successfully created.'' format.html { redirect_to(@product) } format.xml { render :xml => @product, :status => :created, :location => @product } else format.html { render :action => "new" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end
这是我的Java代码。我应该在URL名称中传递数据,还是必须将其设置在其他位置?(也许是httpost.setEntity吗?)最终,我将使用JSON,但现在我只想获取它,以便实际上可以在Rails中调用“
create”方法。Rails正在获取POST,但从不执行“ create”方法中的任何代码
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.0.100:3000/products/new"); HttpResponse response = httpclient.execute(httppost);
我很困惑,如果有人能指出我正确的方向,我将不胜感激。
答案1
小编典典我将以下内容添加到我的POST请求中,它就像一个魅力一样工作。
httppost.addHeader("Content-Type","application/json");
JRuby on Rails:在Rails应用程序中使用自定义Java类
例如,假设我创建了Dog.java:
class Dog { private String name; public Dog() { name = "Fido"; } public String getName() { return name; } }
我怎样才能在我的Rails应用程序中创建一个新的Dog对象(Dog.new)?我需要将Dog.java或Dog.class文件放在某处,然后调用某种形式的“import”将其导入我的Rails应用程序.我不知道这应该放在目录结构中,也不知道我应该在哪里以及如何告诉我的应用程序如何包含它.
解决方法
>编译课程.
mkdir classes javac -d classes src/Dog.java
>在Rails应用程序中添加类到类路径(例如,初始化程序).
require 'java' $CLAsspATH << File.join(Rails.root,"classes")
>导入课程.
java_import Java::Dog
如果你想用Warbler制作你的Rails应用程序的war文件,你也可以使用config / warble.rb中的config.dirs选项将classes目录添加到war文件中,并且Dog类将可用而无需添加到$CLAsspATH,因为Java约定将WEB-INF / classes添加到Java Web应用程序的类路径中.
ruby – 在rails 3中创建新应用程序时运行bundle install
这需要相当长的时间才能完成.每次创建应用程序时都需要运行bundle install,如果没有,是否有任何方法可以阻止它在每次创建新的rails应用程序时运行,以便rails new快速完成?
解决方法
ruby-on-rails – Postgres不会使用c9.io连接到Ruby on Rails应用程序中的服务器
我重新启动了我的服务器,但是当我尝试访问我的应用程序时,出现此错误:
PG::ConnectionBad Could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGsql.5432"?
我在SO上看过几个类似的回答问题,但它们都涉及Mac.由于我使用的是Cloud 9 IDE,我认为可能存在差异(例如,localhost的提及可能不相关,因为这不是我访问当前在c9.io上的应用程序的方式.)
根据其他问题的答案,我在我的终端尝试了这个:
sudo ps auxw | grep post
这给了我:
ubuntu 2527 0.0 0.0 10552 888 pts/5 S+ 15:21 0:00 grep --color=auto post
然后我试过:
sudo find / -name .s.PGsql.5432 -ls
我得到了:
5286 0 lrwxrwxrwx 1 root root 26 May 11 15:13 /var/pgsql_socket/.s.PGsql.5432 -> /private/tmp/.s.PGsql.5432 5183 0 srwxrwxrwx 1 postgres postgres 0 May 10 22:18 /run/postgresql/.s.PGsql.5432 yamilethmedina@wheels_registration:~/workspace (master|MERGING) $psql psql: Could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGsql.5432"?
这是我的服务器的完整跟踪:
Started GET "/" for 12.43.117.2 at 2015-05-11 16:35:48 +0000 PG::ConnectionBad (Could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGsql.5432"? ): activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `initialize' activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `new' activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect' activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize' activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new' activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout' /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection' /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection' activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection' activerecord (4.1.6) lib/active_record/connection_handling.rb:113:in `retrieve_connection' activerecord (4.1.6) lib/active_record/connection_handling.rb:87:in `connection' activerecord (4.1.6) lib/active_record/migration.rb:388:in `check_pending!' activerecord (4.1.6) lib/active_record/migration.rb:377:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks' actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app' railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged' railties (4.1.6) lib/rails/rack/logger.rb:20:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' rack (1.5.2) lib/rack/runtime.rb:17:in `call' activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call' rack (1.5.2) lib/rack/sendfile.rb:112:in `call' railties (4.1.6) lib/rails/engine.rb:514:in `call' railties (4.1.6) lib/rails/application.rb:144:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' rack (1.5.2) lib/rack/content_length.rb:14:in `call' rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service' /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run' /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread' Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms) Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms) Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.9ms) Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (45.7ms)
我的整个github回购以防万一:
https://github.com/yamilethmedina/wheels_registration.git
解决方法
尝试
sudo service postgresql start
并查看它是否返回,并确保关闭vm(因此停止脚本可以运行),而不是仅关闭它.
ruby-on-rails – RACK_ENV在Rails应用程序中做了什么?
... export RACK_ENV=none export RAILS_ENV=production ...
RACK_ENV =没有做什么?我无法在任何地方找到相关文档.我是否需要在Rails应用程序中设置它,或者我可以删除该导出?
解决方法
要查找当前环境,Rails应用程序首先查找RAILS_ENV环境变量,然后对于RACK_ENV环境变量,则默认为“development”.
今天关于Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”和java通过http调用接口的讲解已经结束,谢谢您的阅读,如果想了解更多关于JRuby on Rails:在Rails应用程序中使用自定义Java类、ruby – 在rails 3中创建新应用程序时运行bundle install、ruby-on-rails – Postgres不会使用c9.io连接到Ruby on Rails应用程序中的服务器、ruby-on-rails – RACK_ENV在Rails应用程序中做了什么?的相关知识,请在本站搜索。
本文标签: