Ruby on Rails pass id to new create form

╄→гoц情女王★ 提交于 2019-12-05 21:54:50

When you click the New invoice link on the /views/accounts/show page, I suppose that you want that your new invoice belongs to this account.

So in your form, you don't have to let the user choose an account. You can for example replace the corresponding field by a hidden_field:

<%= f.hidden_field :account_id, :value => params[:account_id] %>

Also in the new action of your controller, replace @invoice = Invoice.new(:account_id => params[:account_id]) by @invoice = Invoice.new

Hope this helps.

you did not post the code of your form, but i guess that you are using a text-field for handling the account association. THIS IS WRONG!

if you use a text-field, then rails will try storing it as a string => Account(#2281084000) expected, got String(#2267210740)

you need to use some kind of relational field like a dropdown or whatever to select one of the accounts that are already there.

there are tons of good examples out there, this might help you: http://railscasts.com/episodes/102-auto-complete-association-revised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!