Hello, Is there a way to use related objects within Form Helpers? Right now I have an Application object that belongs_to a User. I''d like to do something like: <%= text_field("application.user", "first_name") %> When I try this, I get the error "''@application.user'' is not allowed as an instance variable name". So my question is this, can the user object be assigned into the Form Helper without manually assigning them to instance variables inside the controller first? Thanks for the help, -- DeLynn Berry delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org
My current method, but I''m rather new to rails, is to add inside the controller using a begin_filter thereby isolating it to one line... if there is a better solution, i dont know about it but I''d love to hear it. DeLynn Berry wrote:>Hello, > >Is there a way to use related objects within Form Helpers? Right now I >have an Application object that belongs_to a User. I''d like to do >something like: > ><%= text_field("application.user", "first_name") %> > >When I try this, I get the error "''@application.user'' is not allowed as >an instance variable name". So my question is this, can the user object >be assigned into the Form Helper without manually assigning them to >instance variables inside the controller first? > >Thanks for the help, > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
"DeLynn Berry" <delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org> writes:> <%= text_field("application.user", "first_name") %>I think you could do <%= test_field("user", "first_name") %> and then in the controller just instantiate a user User.new(@params[''user'']). Once you have a valid user you can assign the application.user = user. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
I posted a question a couple of weeks ago about this same issue and didn''t get a response, I think it would be a worthy feature though it could be vulnerable to tampering if proper precautions aren''t being used if you expose your entire model. In the webwork2 mvc framework for java this can be used, and I believe Spring-mvc can also do this, just pass in user.billingInfo.address.street, etc.. was expecting rails to behave similarly in rails using something like user[billingInfo][address][street] Doug Alcorn wrote:>"DeLynn Berry" <delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org> writes: > > > >><%= text_field("application.user", "first_name") %> >> >> > >I think you could do <%= test_field("user", "first_name") %> and then >in the controller just instantiate a user >User.new(@params[''user'']). Once you have a valid user you can assign >the application.user = user. > >
Francisco Hernandez <lagcisco-b7MHZcQsHeJWk0Htik3J/w@public.gmane.org> writes:> In the webwork2 mvc framework for java this can be used, and I believe > Spring-mvc can also do this, just pass in > user.billingInfo.address.street, etc.. > was expecting rails to behave similarly in rails using something like > user[billingInfo][address][street]When you call update_attributes, the model will call attribute= for anything that''s in the parameter hash. So: billing = BillingInfo.new billing.update_attributes({address_street => "123 Anystreet"}) will in turn call BillingInfo#address_street=. It doesn''t matter if address_street is an attribute or not; just whether BillingInfo has an address_street= method. I''m exactly sure how to leverage this to get associations updated with form helpers though. Here''s what I''ve done, but I''m not sure if it''s the best solution. class Hobby < ActiveRecord::Base end Class MemberProfile < ActiveRecord::Base has_and_belongs_to_many :hobbies, :uniq => true def hobbies_ids=(list) hobbies.clear hobbies << list.collect { |o| Hobby.find(o) } end end and then in the view I have: <% Hobby.option_list.each do |o| %> <% name = o.first; id = o.last %> <input type="checkbox" id="<%= id %>" name="member_profile[hobbies_ids][]" value="<%= id %>" <% if @member_profile.hobby_by_name?(name) %> checked="checked" <% end %> /><%= name %> <% end %></p> That gives my controller a list of ids as strings in @params[''member_profile''][''hobbies_ids'']. When my controller calls @member_profile.update_attributes(@params[''member_profile'']) the hobbies_ids= method converts that list of string ids into a list of objects. I definitely think we need more examples of how to update associations on the wiki. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org