sorry for the noob question: how do i reference an association''s properties in a form helper? e.g. if i wanted to build a select list to choose a user''s address''s state (user.address.state), how would i hook that to the "method" of collection_select collection_select :user, ???, States.find(:all)
Mark Reginald James
2006-Jul-21 06:16 UTC
[Rails] Re: using association properties in form helpers
jeff emminger wrote:> sorry for the noob question: > > how do i reference an association''s properties in a form helper? > > e.g. if i wanted to build a select list to choose a user''s address''s > state (user.address.state), how would i hook that to the "method" of > collection_select > collection_select :user, ???, States.find(:all)No, you can''t do that directly at present, though a core patch is available to allow cascades such as this to be used in form helpers: http://dev.rubyonrails.org/ticket/2053 Instead you need to write: Controller: for new: @user = User.new(params[:user]) @address = @user.build_address(params[:address]) or for edit: @user = User.find(params[:id]) @address = @user.address @address.attributes = params[:address] View: collection_select :address, :state, ... -- We develop, watch us RoR, in numbers too big to ignore.
jeff emminger
2006-Jul-22 17:29 UTC
[Rails] Re: using association properties in form helpers
awesome - thanks for the reply. i kind of figured out the same answer but it''s good to hear reinforcement. makes for a bit of a mess in the update method unfortunately. that core patch - is it going to be made a part of a future release e.g. RoR 1.7 or something? On 7/21/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> jeff emminger wrote: > > sorry for the noob question: > > > > how do i reference an association''s properties in a form helper? > > > > e.g. if i wanted to build a select list to choose a user''s address''s > > state (user.address.state), how would i hook that to the "method" of > > collection_select > > collection_select :user, ???, States.find(:all) > > No, you can''t do that directly at present, though a core patch > is available to allow cascades such as this to be used in form > helpers: http://dev.rubyonrails.org/ticket/2053 > > Instead you need to write: > > Controller: > > for new: > > @user = User.new(params[:user]) > @address = @user.build_address(params[:address]) > > or for edit: > > @user = User.find(params[:id]) > @address = @user.address > @address.attributes = params[:address] > > View: > > collection_select :address, :state, ... > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >