如何在 Rails 3 中通过嵌套表单向下传递外键属性
我有三个模型:
class Client < ActiveRecord::Base
has_many :balancesheets
has_many :investment_assets, :through => :balancesheets
class Balancesheet < ActiveRecord::Base
belongs_to :client
has_many :investment_assets, :dependent => :destroy
accepts_nested_attributes_for :investment_assets, :allow_destroy => true
class InvestmentAsset < ActiveRecord::Base
belongs_to :balancesheet
belongs_to :client
我有两个与外键 client_id 有关的问题。首先,当我创建新的资产负债表时,我使用 collection_select 从列表中选择客户。我宁愿将新的资产负债表链接放在客户显示页面上,然后将客户 ID 传递到表单,这样我就不必从列表中选择客户或在字段中键入客户。首先,我该怎么做?
其次,投资资产模型嵌套在资产负债表形式中。一切工作正常,除了 Investment_asset 的 client_id 属性为空。我不知道为什么,因为我的联想似乎没问题。所以问题是,如何通过嵌套表单传递这个 client_id 属性?我可以发布模型或控制器,请告诉我。
更新我已经在这里。但是,我仍在尝试根据该答案找出第二部分,即如何通过嵌套表单传递此 client_id ?为了展示创建资产负债表时传递用户 ID 的工作原理,这里是客户端显示页面链接:
<%= link_to 'New BalanceSheet',new_balancesheet_path(:id => @client.id) %>
在资产负债表表单中,我有一个隐藏字段:
<%= f.hidden_field :client_id %>
在资产负债表控制器中,这是新操作:
@balancesheet = Balancesheet.new(:client_id => params[:id])
这工作得很好。因此,对于 Investment_asset,我使用 Ryan Bates Nested_form gem ,它的链接语法略有不同。因此,在资产负债表表单内,添加新的investment_asset的链接是:
<%= f.link_to_add "Add asset", :investment_assets %>
我无法弄清楚如何像在资产负债表上那样传递用户ID:
(:id => @client.id)
我可以将相同的内容放入investment_asset控制器中的新操作中并添加隐藏字段,但我不确定如何将其传递到链接上。想法?
I have three models:
class Client < ActiveRecord::Base
has_many :balancesheets
has_many :investment_assets, :through => :balancesheets
class Balancesheet < ActiveRecord::Base
belongs_to :client
has_many :investment_assets, :dependent => :destroy
accepts_nested_attributes_for :investment_assets, :allow_destroy => true
class InvestmentAsset < ActiveRecord::Base
belongs_to :balancesheet
belongs_to :client
I have two questions that have to do with the foreign key client_id. First, when I create a new balancesheet, I use collection_select to select the client from the list. I would rather put the new balancesheet link on the client show page and just pass the client id through to the form so I don't have to choose a client from a list or type it in a field. So first, how do I do that?
Second, the investment_asset model is nested in the balancesheet form. Everything works just fine, except the client_id attribute for the investment_asset is blank. Im not sure why because my associations seem to be okay. So the question is, how do I pass this client_id attribute down through the nested form? I can post models or controllers, just let me know.
UPDATE I have since found the answer to my first question here. However, I am still trying to figure out the second part based on that answer, which is, how do I pass this client_id down through a nested form? To show how it worked passing user id when creating a balancesheet, here is the client show page link:
<%= link_to 'New BalanceSheet',new_balancesheet_path(:id => @client.id) %>
In the balancesheet form I have a hidden field:
<%= f.hidden_field :client_id %>
And in the Balancesheet Controller this is the new action:
@balancesheet = Balancesheet.new(:client_id => params[:id])
This works just fine. So for an investment_asset I am using Ryan Bates nested_form gem which has a little bit different syntax for a link. So inside the balancesheet form the link to add a new investment_asset is:
<%= f.link_to_add "Add asset", :investment_assets %>
I can't figure out how to pass the user id like I did on the balancesheet with:
(:id => @client.id)
I can put the same thing in a new action in the investment_asset controller and add a hidden field, but I'm not sure how to pass it in on the link. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我所知,这可能是一个彻底的黑客攻击,但我所知道的非常有限。我最终在这里做的是用户 jQuery 从资产负债表中获取 id 并将其强制放入 Investment_asset 的隐藏字段中。 jQuery:
然后我的隐藏字段看起来像这样:
它工作得很好。希望这是一种有效的方法。
This might be a total hack for all I know, but all I know is very limited. What I ended up doing here was user jQuery to take the id from the balancesheet and force it into a hidden field for investment_asset. jQuery:
Then my hidden field looks like this:
It works just fine. Hopefully that's a valid way of doing it.