如何通过Rails 3中的嵌套窗体传递外键属性 [英] How to pass foreign key attributes down through a nested form in Rails 3

查看:117
本文介绍了如何通过Rails 3中的嵌套窗体传递外键属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型:

  class Client< ActiveRecord :: Base 
has_many:balancesheets
has_many:investment_assets,:through => :余额表

类余额表< ActiveRecord :: Base
belongs_to:client
has_many:investment_assets,:dependent => :销毁
accept_nested_attributes_for:investment_assets,:allow_destroy => true

class InvestmentAsset< ActiveRecord :: Base
belongs_to:balancesheet
belongs_to:client

我有两个与外键client_id有关的问题。首先,当我创建新的资产负债表时,我使用collection_select从列表中选择客户端。我宁愿将新的balancesheet链接放在客户端显示页面上,只是将客户端ID传递给表单,所以我不必从列表中选择一个客户端或在一个字段中键入它。首先,我该怎么做?

其次,investment_asset模型嵌套在balancesheet表单中。一切正常,除了投资_asset的client_id属性是空的。我不知道为什么,因为我的协会似乎是好的。所以问题是,我如何通过嵌套窗体传递这个client_id属性?我可以发布模型或控制器,只是让我知道。



更新我已经找到了我的第一个问题的答案here 。但是,我仍然试图根据这个答案来弄清楚第二部分,也就是说,如何通过嵌套形式传递这个client_id?为了说明如何在创建结算表时传递用户标识,这里是客户端显示页面链接:

 <%= link_to 'New BalanceSheet',new_balancesheet_path(:id => @ client.id)%> 

在balancesheet表单中我有一个隐藏字段:

 <%= f.hidden_​​field:client_id%> 

而在资产负债表控制器中,这是新的操作:

  @balancesheet = Balancesheet.new(:client_id => params [:id])

这工作得很好。所以对于一个investment_asset,我使用 Ryan Bates nested_form gem ,这个链接的语法略有不同。所以在balancesheet里面添加一个新的链接就是:

 <%= f.link_to_addAdd asset, :investment_assets%> 

我不知道如何在balancesheet上传递用户id / p>

 (:id => @ client.id)

我可以将相同的东西放在投资控制器的新行为中,并添加一个隐藏字段,但是我不确定如何在链接上传递它。想法?

解决方案

这可能是我所知道的全部破解,但我所知道的是非常有限的。我最终在这里做的是用户jQuery从资产负债表中获取id,并将其强制为一个隐藏字段for investment_asset。 jQuery:
$ b $ pre $ //从balancesheet隐藏字段中获取包含user_id值的值
var balsheetclientid = jQuery '#hiddenid')ATTR( '值')。
//将其插入隐藏字段中的value属性中,类名为.iaclientid
jQuery('。iaclientid')。val(balsheetclientid)
pre>

然后我的隐藏字段如下所示:

 < %= f.hidden_​​field:client_id,:class => 'iaclientid',:value => ''%> 

工作得很好。希望这是一个有效的方法。


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?

解决方案

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:

// get the value from the balancesheet hidden field that contains the user_id value
var balsheetclientid = jQuery('#hiddenid').attr('value'); 
// insert it into the value attribute in the hidden field with class name .iaclientid
jQuery('.iaclientid').val(balsheetclientid) 

Then my hidden field looks like this:

<%= f.hidden_field :client_id, :class => 'iaclientid', :value => '' %> 

It works just fine. Hopefully that's a valid way of doing it.

这篇关于如何通过Rails 3中的嵌套窗体传递外键属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆