Rails 3 嵌套资源变量
我有一些与我正在创建的 Transaction 对象相关的问题。
交易属于贷款且贷款有很多交易。
因此,我设置了一个嵌套路由:
resources :loans do
resources :transactions
end
我的问题是:如何将贷款值传递到交易的“loan_id”字段中?这最好在控制器中完成还是作为表单中的隐藏字段完成?嵌套路由是否创建了一种获取此变量的简单方法?
我以为这会自动完成,但当我按原样保存时,该字段为空。
任何帮助将不胜感激!
I have a few questions relating to a Transaction object that I'm creating.
Transaction belongs_to Loan and Loan has_many Transactions.
Therefore, I setup a nested route:
resources :loans do
resources :transactions
end
My question is: How do I pass the loan value into the Transaction's 'loan_id' field? Is this best done in the controller or as a hidden_field in the form? Does the nested route create an easy way to grab this variable?
I assumed this would be done automatically, but the field is empty when I saved it as-is.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您调用特定交易,
新
交易的路线将是您可以使用模型关联,如下所示:在您的
create
操作中:这样您的新
@交易
将已经填充有loan_idif you call a specific transaction, the route for a
new
transaction will beyou could use model association like this: in your
create
action:that way your new
@transaction
will be already populated with the loan_id考虑将
before_filter
添加到您的控制器,并让它调用私有方法来获取所有操作的:id
。将其放在transactions
控制器的顶部:然后在所有操作之后添加:
在您的
new
操作中像这样使用它:Consider adding a
before_filter
to your controller and having it call a private method to grab the:id
across all actions. Place this at the top of yourtransactions
controller:And then after all the actions, add:
Use it like this in your
new
action: