I''ve got a bunch of form fields that are mostly normal: <%= text_field ''car'', ''maker'', :size => 40 %> <%= text_field ''car'', ''owner'', :size => 40 %> <%= text_field ''car'', ''color'', :size => 40 %> All these attributes are in the cars table. But one of the fields, ''price'', is stored in a different table: <%= text_field ''car'', ''price'', :size => 40 %> ''price'' is an attribute of the financials table. How can I have the form submission update the field in this second table? I tried changing the param name to this: <%= text_field ''car[financials]'', ''price'', :size => 40 %> ...but that didn''t work. I know this is weird, but the client has it this way due to legacy reasons, and I can''t change the schema right now. Any and all suggestions are appreciated. Thanks! -Jason PS: tables and attributes have been changed to protect the innocent. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
this is the easiest way i know of:
controller:
def edit_car
@car = Car.find(params[:id])
@financial = @car.financial #im assuming there is some sort of
relationship here
end
def update_car
@car = Car.find(params[:id])
@financial = @car.financial
if @car.update_attributes(params[:car])
@financial.update_attributes(params[:financial])
flash[:notice] = "car saved"
redirect_to :action => "edit_car", :id => @car.id
else
flash[:warning] = "car not saved"
redirect_to :action => "edit_car", :id => @car.id
end
end
view:
<%= text_field ''car'', ''maker'', :size
=> 40 %>
<%= text_field ''car'', ''owner'', :size
=> 40 %>
<%= text_field ''car'', ''color'', :size
=> 40 %>
<%= text_field ''financial'', ''price'',
:size => 40 %>
there is probably a leaner way to do that.
--jake
Jason Frankovitz wrote:> I''ve got a bunch of form fields that are mostly normal:
>
> <%= text_field ''car'', ''maker'',
:size => 40 %>
> <%= text_field ''car'', ''owner'',
:size => 40 %>
> <%= text_field ''car'', ''color'',
:size => 40 %>
>
> All these attributes are in the cars table. But one of the fields,
> ''price'', is stored in a different table:
>
> <%= text_field ''car'', ''price'',
:size => 40 %>
>
> ''price'' is an attribute of the financials table. How can
I have the
> form submission update the field in this second table? I tried
> changing the param name to this:
>
> <%= text_field ''car[financials]'',
''price'', :size => 40 %>
>
> ...but that didn''t work. I know this is weird, but the client has
it
> this way due to legacy reasons, and I can''t change the schema
right
> now. Any and all suggestions are appreciated.
>
> Thanks!
> -Jason
>
> PS: tables and attributes have been changed to protect the innocent.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
This is essentially the approach I am using at the moment, but I am
convinced it can be done with the right text_field names if I could
only work it out!
Something like:
CarController ---
def update
if params[:car] # Make sure the form has been submitted
begin # Make sure the car exists
@car = Car.find(params[:id])
rescue
flash[:notice] = "Car not found"
redirect_to :action => "index"and return # Stop script
end
if @car.update_attributes(params[:car])
flash[:notice] = "Car details updated"
redirect_to :action => "edit_car", :id => @car.id and return
# Stop
processing the script.
else
flash[:warning] = "Car not saved"
redirect_to :action => "edit_car", :id => @car.id
end
end
view:
<%= text_field ''car'', ''maker'', :size
=> 40 %>
<%= text_field ''car'', ''owner'', :size
=> 40 %>
<%= text_field ''car'', ''color'', :size
=> 40 %>
<%= text_field ''car.financial'',
''price'', :size => 40 %> # This is the
bit I have not worked out yet. It may not be possible.
Can anyone tell me if this is possible and what the text_field might
look like?
On Dec 22, 10:28 am, Jake Varghese
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> this is the easiest way i know of:
>
> controller:
>
> def edit_car
> @car = Car.find(params[:id])
> @financial = @car.financial #im assuming there is some sort of
> relationship here
> end
>
> def update_car
> @car = Car.find(params[:id])
> @financial = @car.financial
> if @car.update_attributes(params[:car])
> @financial.update_attributes(params[:financial])
> flash[:notice] = "car saved"
> redirect_to :action => "edit_car", :id => @car.id
> else
> flash[:warning] = "car not saved"
> redirect_to :action => "edit_car", :id => @car.id
> end
> end
>
> view:
> <%= text_field ''car'', ''maker'',
:size => 40 %>
> <%= text_field ''car'', ''owner'',
:size => 40 %>
> <%= text_field ''car'', ''color'',
:size => 40 %>
> <%= text_field ''financial'',
''price'', :size => 40 %>
>
> there is probably a leaner way to do that.
>
> --jake
>
>
>
> Jason Frankovitz wrote:
> > I''ve got a bunch of form fields that are mostly normal:
>
> > <%= text_field ''car'',
''maker'', :size => 40 %>
> > <%= text_field ''car'',
''owner'', :size => 40 %>
> > <%= text_field ''car'',
''color'', :size => 40 %>
>
> > All these attributes are in the cars table. But one of the fields,
> > ''price'', is stored in a different table:
>
> > <%= text_field ''car'',
''price'', :size => 40 %>
>
> > ''price'' is an attribute of the financials table. How
can I have the
> > form submission update the field in this second table? I tried
> > changing the param name to this:
>
> > <%= text_field ''car[financials]'',
''price'', :size => 40 %>
>
> > ...but that didn''t work. I know this is weird, but the client
has it
> > this way due to legacy reasons, and I can''t change the schema
right
> > now. Any and all suggestions are appreciated.
>
> > Thanks!
> > -Jason
>
> > PS: tables and attributes have been changed to protect the innocent.--
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---