How to pass foreign key attributes down through a

2019-07-30 13:40发布

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?

1条回答
Ridiculous、
2楼-- · 2019-07-30 14:07

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.

查看更多
登录 后发表回答