我的设置:Rails 3.0.9,Ruby 1.9.2
我希望只为create动作生成一个脚手架,它的语法是什么?我猜这是我追加的东西
rails g scaffold Project name:string ...
Run Code Online (Sandbox Code Playgroud) 我的设置:Rails 3.0.9,Ruby 1.9.2
我向嵌套资源任务添加了自定义操作.
routes.rb
resources :tasks
resources :projects do
resources :tasks, :constraints => { :protocol => "http" } do
put :cancel, :on => :member
end
end
Run Code Online (Sandbox Code Playgroud)
rake routes 节目
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,
tasks_controller.rb
def cancel
@task = Task.find(params[:id])
respond_to do |format|
if @task.cancel
format.html { redirect_to(@task, :notice => 'Task was successfully canceled.') }
else
format.html { render :action => "edit" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我需要定义一个表单来执行操作,这就是我目前所拥有的
_form.html.erb for subscription
<%= form_for [@project, @task], :url => { …Run Code Online (Sandbox Code Playgroud) 我的设置:Rails 3.0.9,Ruby 1.9.2
我有理由这样做,但我需要的是一种动态添加虚拟属性到activerecord结果集的方法.这意味着我没有attr_accessor在模型中使用,而是希望动态地将虚拟属性添加到结果集中.
例如,
users = User.all
#a user has following attributes: name, email, password
Run Code Online (Sandbox Code Playgroud)
我喜欢做的是说增加(不使用attr_accessor)虚拟属性status来users,这可能吗?
我有一个Play 2.0应用程序
TestController.scala
def foo(p1: String) = Action {implicit request =>
Ok(bar(p1))
}
private def bar(p1: String) = {
//access request parameter here
}
Run Code Online (Sandbox Code Playgroud)
有没有办法implicit用来传递request给bar
我在Rails 2.3.5上.在典型的用户控制器中创建操作
class UsersController
def create
@user = User.new(params[:user])
respond_to do |format]
if @user.save ...
else
format.json ....
end
end
end
Run Code Online (Sandbox Code Playgroud)
当客户端传递无效/格式错误的JSON字符串进行输入时,Rails会抛出500内部服务器错误,说"无效的JSON字符串".我可以捕获错误,以便我可以给出自定义消息吗?
更新这里是请求的堆栈跟踪.就这样我很清楚,我知道这是一个格式错误的JSON字符串,我的问题不是如何修复JSON字符串而是如何捕获此特定错误,以便我可以发回比HTTP 500内部服务器错误更有意义的错误消息.在此先感谢您的帮助
Error occurred while parsing request parameters.
Contents:
user: {login: "John", email: "john@yahoo.com", password: "111"}}
/!\ FAILSAFE /!\ Mon Nov 08 02:01:04 -0800 2010
Status: 500 Internal Server Error
Invalid JSON string
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/json/backends/yaml.rb:14:in `decode'
c:1:in `__send__'
c:1:in `decode'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:42:in `parse_formatted_parameters'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:11:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in `call'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in `run' …Run Code Online (Sandbox Code Playgroud) 我的设置:Rails 2.3.10,Ruby 1.8.7
我在几个模型之间有一套相当复杂的关系.
class A
has_many :classB
has_many :classD
end
class B
belongs_to :classA
has_many :classC
end
class C
belongs_to :classB
belongs_to :classE
end
class D
belongs_to :classA
belongs_to :classE
end
class E
has_many :classD
has_many :classC
end
Run Code Online (Sandbox Code Playgroud)
我遇到了使用JSON语法的问题,以获取以classA开头的所有相关信息.这是我到目前为止所做的工作.
classA.to_json(:include => {:classB => {:include => [:classC, :classE]}})
Run Code Online (Sandbox Code Playgroud)
我无法使语法工作也包括classD和相关的classE记录.
更新实际上这样的东西可能会起作用,除了我不能混合哈希和数组
classA.to_json(:include => [ :classB => { :include => { :classC => { :include => :classE } } },
:classD, :classE ])
Run Code Online (Sandbox Code Playgroud)
请注意,我在上面的示例代码中没有使用单数/复数,但在我的实际代码中,我是.任何见解将不胜感激.
谢谢,鲍勃
我遇到了使用Jerkson在Scala中处理JSON的优秀教程.特别是,我有兴趣将JSON反序列化为用户定义的case类.这篇文章有一个简单的例子
case class Simple(val foo: String, val bar: List[String], val baz: Map[String,Int])
object SimpleExample {
def main(args: Array[String]) {
import com.codahale.jerkson.Json._
val simpleJson = """{"foo":42, "bar":["a","b","c"], "baz":{"x":1,"y":2}}"""
val simpleObject = parse[Simple](simpleJson)
println(simpleObject)
}
}
Run Code Online (Sandbox Code Playgroud)
运行它时出现此错误,我在Play 2.0.1,Scala 2.9.1-1,Jerkson 0.5.0.
Execution exception [[ParsingException: Unable to find a case accessor
Run Code Online (Sandbox Code Playgroud)
在Google网上论坛中也发现了这一点,但没有帮助.
有任何想法吗?
所以我有一个相当负载的env变量 _JAVA_OPTIONS
export _JAVA_OPTIONS="-Dhttp.proxyHost=my-proxy.com -Dhttp.proxyPort=1080
-Dhttps.proxyHost=my-proxy.com -Dhttps.proxyPort=1080
-DsocksProxyHost=my-socks-proxy.com
-Dhttp.nonProxyHosts=\"localhost|127.0.0.1|*.local|*.my-co.com\""
Run Code Online (Sandbox Code Playgroud)
但是当我使用Apache HTTP客户端API从Scala代码尝试时,我无法忽略内部服务器URL.
https://username:pwd@server.my-co.com/foo/bar
Run Code Online (Sandbox Code Playgroud)
我是否需要nonProxyHosts为HTTPS 指定其他内容?文档没有指定这样的参数.我错过了什么?我在Mac上.
我刚刚向Heroku部署了一些代码和数据库更改,我想将其回滚.我确实有发布附加组件,并且能够回滚代码而不是数据库.我没有备份数据库,所以我无法进行恢复.我尝试了"heroku rake db:rollback",但它没有做任何事情.有谁知道?