Bob Smith
2010-May-31 05:25 UTC
Trying to make new instance variables for each item in render :partial
I have a list of people I am using as the collection for a render :partial. This is for a list of users that is being made into a pdf. I need to do 3 find calls to find out their attendance for the last 3 years. The only way I can find to do this is to have the print function call its view and have the find code in there, but I know that''s wrong.. Please point me in the right direction. Maybe a way to put the login in the controller ?? Is there a way to add some logic to the beginning of a render ?? def print debugger @today = Date.today @year = @today.year @households = Household.find(:all, :order => "last_name, first_name") @churches = Church.find(:all, :order => "name").map {|u| [u.name, u.id]} respond_to do |format| # format.html # index.html # format.xml { head :ok } # format.pdf { send_data render_to_pdf({ :action => ''log'', :layout => ''pdf_report'' }) } format.pdf { send_data render_to_pdf( :action => ''print'', :layout => ''pdf_report''), :filename => "households for " + Time.now.to_s(:long) + ".pdf" } end end <% @households.each do |@household| %> <% @thisyear Visit.find_or_create_by_household_id_and_year(:household_id => @household, :year => @year) %> <% @lastyear Visit.find_or_create_by_household_id_and_year(:household_id => @household, :year => @year-1) %> <% @twoyearsago Visit.find_or_create_by_household_id_and_year(:household_id => @household, :year => @year-2) %> <%= render :partial=>"print" %> <% end %> -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-31 08:26 UTC
Re: Trying to make new instance variables for each item in render :partial
On May 31, 6:25 am, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a list of people I am using as the collection for a > render :partial. This is for a list of users that is being made into a > pdf. I need to do 3 find calls to find out their attendance for the > last 3 years. The only way I can find to do this is to have the print > function call its view and have the find code in there, but I know > that''s wrong.. Please point me in the right direction. Maybe a way to > put the login in the controller ?? Is there a way to add some logic to > the beginning of a render ??I think you want to restructure your code a bit. For example, create an instance method on household for retrieving a view object given a year. Assuming you had a helper function or a partial for displaying views your partial for displaying a household wouldn''t need to be much more complicated than <%= format_view household.view_for_year @year %> <%= format_view household.view_for_year @year - 1%> <%= format_view household.view_for_year @year - 2%> fred> > def print > debugger > @today = Date.today > @year = @today.year > @households = Household.find(:all, :order => "last_name, > first_name") > @churches = Church.find(:all, :order => "name").map {|u| [u.name, > u.id]} > > respond_to do |format| > # format.html # index.html > # format.xml { head :ok } > # format.pdf { send_data render_to_pdf({ :action => > ''log'', :layout => ''pdf_report'' }) } > format.pdf { send_data render_to_pdf( :action => ''print'', > :layout => > ''pdf_report''), > :filename => "households > for " + Time.now.to_s(:long) + ".pdf" } > end > end > > <% @households.each do |@household| %> > <% @thisyear > Visit.find_or_create_by_household_id_and_year(:household_id => > @household, :year => @year) %> > <% @lastyear > Visit.find_or_create_by_household_id_and_year(:household_id => > @household, :year => @year-1) %> > <% @twoyearsago > Visit.find_or_create_by_household_id_and_year(:household_id => > @household, :year => @year-2) %> > <%= render :partial=>"print" %> > <% end %>-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.