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: <h1>Editing degree</h1> <%= error_messages_for :degree %> <%= start_form_tag :action => ''update'', :id => params[:id] %> <!--[form:degree]--> <p><label for="degree_title">Title</label><br/> <%= text_field ''degree'', ''title'' %></p> <p><label for="degree_detail">Detail</label><br/> <%= text_area ''degree'', ''detail'' %></p> <p><label for="degree_careers">Careers</label><br/> <%= text_area ''degree'', ''careers'' %></p> <p><label for="degree_contact">Contact</label><br/> <%= text_field ''degree'', ''contact'' %></p> <!--[eoform:degree]--> <p><label for="assigned_grade">Contact</label><br/> <% for @assigned in @degree.assigneds %> <%= error_messages_for :assigned %> <% fields_for "assigned" do |f| %> <p><%= f.text_field :grade %></p> <%@quals=Qual.find(:all)%> <p><label for="assigned_qual_id">Qualification ID</label><br/> <%=collection_select :assigned, :qual_id, @quals,:id, :screen_qual%><br /> <% 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 http://www.w3.org/2001/tag/doc/whenToUseGet.html) 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 -- 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 -~----------~----~----~----~------~----~------~--~---
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 http://www.w3.org/2001/tag/doc/whenToUseGet.html) 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 -- 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 -~----------~----~----~----~------~----~------~--~---
Not to worry seem to have sorted it --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 7/26/07, Matt_99 <m.davis-pyHcBy/D9uhaa/9Udqfwiw@public.gmane.org> wrote:> > > Not to worry seem to have sorted it > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jul 26, 9:22 am, "Ivor Paul" <ivorp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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: http://blog.jayfields.com/2007/03/rails-presenter-pattern.html It basically wraps/aggregates several models into one. It makes your controller leaner, too. Cheers Martin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''m looking to try and get the edit function working on a page for editing multiple models within the one form. I can currently edit any child models of a parent if the child records already exist, the problem lies with adding extra child records to the parent record. My update function currently looks like this: def update @script = Script.find(params[:id]) if params[:script_metadatas] params[:script_metadatas].each_with_index do |value, index| script_metadata @script.script_metadatas.find_or_initialize_by_id(params[:script_metadatas][index.to_s] ? params[:script_metadatas][index.to_s][''id''] : nil) if script_metadata.new_record? @script.script_metadatas.create(params[:script_metadatas][index.to_s]) else script_metadata.update_attributes(params[:script_metadatas][index.to_s]) end end end end end My view is such: <% form_tag :action => ''update'', :id => @script do %> <div id="script_metadatas" align=center> <% @script.script_metadatas.each_with_index do |script_metadata, index| %> <%= render :partial => ''script_metadata_fields'', :locals => { :script_metadata => script_metadata, :index => script_metadata.id } %> <% end %> </div> <div align=right> <%= render :partial => ''add_script_metadata_link'', :locals => { :index => @script.new_script_metadata_id } %> </div> <div align=right> <br /> <%= submit_tag ''Update Metadata Info'' %> </div> <% end %> My ''script_metadata_fields'' partial: <div id="script_metadata_<%= index %>"> <% fields_for "script_metadatas[#{index}]", script_metadata do |f| %> Key: <%= f.text_field :meta_key %> Value: <%= f.text_field :meta_value %> <%= link_to_remote ''Remove'', :url => { :action => ''remove_script_metadata'', :index => index } %> <% end %> </div> The problem i have is when i submit the data, i get the following error message: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[] All the correct parameters seem to be being passed, however the update fails on the line beginning ''params[:script_metadatas].each_with_index do |value, index|'' in my controller action, and the line immediately following it. Can anyone suggest why? -- 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 -~----------~----~----~----~------~----~------~--~---