Editing multiple models in one form

Very new to this but would appreciate a hand as this is driving me mad,
I’m trying to update both degree and assigned from a single form, the
degree section updates fine but assigned just retains the same values,
any help much appreciated!

my edit.rhtml file looks like:

Editing degree

<%= error_messages_for :degree %>

<%= start_form_tag :action => ‘update’, :id => params[:id] %>

Title
<%= text_field 'degree', 'title' %>

Detail
<%= text_area 'degree', 'detail' %>

Careers
<%= text_area 'degree', 'careers' %>

Contact
<%= text_field 'degree', 'contact' %>

Contact
<% for @assigned in @degree.assigneds %> <%= error_messages_for :assigned %> <% fields_for "assigned" do |f| %>

<%= f.text_field :grade %>

<%@quals=Qual.find(:all)%>

Qualification ID
<%=collection_select :assigned, :qual_id, @quals,:id, :screen_qual%>
<% end %> <% end %>

<%= submit_tag “Edit” %>
<%= end_form_tag %>

<%= link_to ‘Show’, :action => ‘show’, :id => @degree %> |
<%= link_to ‘Back’, :action => ‘list’ %>

and my controller file looks like:

class DegreesController < ApplicationController

model :degree
model :assigned
def index
list
render :action => ‘list’
end

GETs should be safe (see

URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }

def list
@degree_pages, @degrees = paginate :degrees, :per_page => 10
end

def show
@degree = Degree.find(params[:id])
end

def new
@degree = Degree.new
@assigned= Assigned.new(@params[:assigned])
end

def create
#@degree = Degree.new(params[:degree])
#@assigned =Assigned.new(@params[:assigned])
#if @degree.save
# @degree.assigneds <<@assigned

 @degree = Degree.new(params[:degree])
 @assigned = @degree.assigneds.build(params[:assigned])
if @degree.save
   redirect_to :action => 'index'
 else
   render :action => 'new'
 end

end

 # flash[:notice] = 'Degree was successfully created.'
  #redirect_to :action => 'list'
#else
 # render :action => 'new'
#end

end

def edit
@degree = Degree.find(params[:id])
end

def update
@degree = Degree.find(params[:id])
@degree.attributes = params[:degree]
@degree.assigneds.each { |t| t.update_attributes[:assigned] }
if @degree.valid? && @degree.assigneds.all?(&:valid?)
@degree.save!
@degree.assigneds.each(&:save!)
redirect_to :action => ‘show’, :id => @degree
else
render :action => ‘edit’
end
end

def destroy
Degree.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end

Sorry the controller should look like this, tried something and then
forgot to change it back

class DegreesController < ApplicationController

model :degree
model :assigned
def index
list
render :action => ‘list’
end

GETs should be safe (see

URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }

def list
@degree_pages, @degrees = paginate :degrees, :per_page => 10
end

def show
@degree = Degree.find(params[:id])
end

def new
@degree = Degree.new
@assigned= Assigned.new(@params[:assigned])
end

def create
#@degree = Degree.new(params[:degree])
#@assigned =Assigned.new(@params[:assigned])
#if @degree.save
# @degree.assigneds <<@assigned

 @degree = Degree.new(params[:degree])
 @assigned = @degree.assigneds.build(params[:assigned])
if @degree.save
   redirect_to :action => 'index'
 else
   render :action => 'new'
 end

end

 # flash[:notice] = 'Degree was successfully created.'
  #redirect_to :action => 'list'
#else
 # render :action => 'new'
#end

end

def edit
@degree = Degree.find(params[:id])
end

def update
@degree = Degree.find(params[:id])
@degree.attributes = params[:degree]
@degree.assigneds.each { |t| t.attributes =
params[:assigned][t.id.to_s] }
if @degree.valid? && @degree.assigneds.all?(&:valid?)
@degree.save!
@degree.assigneds.each(&:save!)
redirect_to :action => ‘show’, :id => @degree
else
render :action => ‘edit’
end
end

def destroy
Degree.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end

Not to worry seem to have sorted it

I would really suggest you look at form_for and then fields_for which
then
allows you to create a subset of the form specific to the second model
that
you want to populate.

agile web development with rails page 491.

Regards
Ivor

On Jul 26, 9:22 am, “Ivor P.” [email protected] wrote:

I would really suggest you look at form_for and then fields_for which then
allows you to create a subset of the form specific to the second model that
you want to populate.

agile web development with rails page 491.

I had the same problem recently and came across this:

It basically wraps/aggregates several models into one. It makes your
controller leaner, too.

Cheers
Martin