Topic: problem with @content_for_layout

hello there all,
for my whole site, i have been useing the application.rhtml
now though, i have an action that i need to have no content except whats in this action
(a print page). Anyway, the controller is called group and the action is called print_reports
but, i don't know where to put the print_reports.rhtml layout that i want to use.
i already have a print_reports.rhtml file, but it is displayed in the content_for_layout part of the rhmtl.

so, how do i do this ?

Re: problem with @content_for_layout

It's actually pretty simple.  Rails let's you specify that you want a view rendered *without* the application.rhtml stuff.  Just do :layout => false

render :controller => 'group', :action => 'print_records', :layout => false

Re: problem with @content_for_layout

ok, i put this in the action of what i want rendered?
i actually put render :action=> 'print_records' in the
def print_records action ?

this action isn't exactly called like the link_to actions in my app, its called from a form in the group action. guy selects the sites that belong_to :group and selects the dates and the form_tag sends it to the group controller where i have the print_reports action.

do i do it the same way ?

by the way danger, you are the guy who helped me earlier today,
thanks

Re: problem with @content_for_layout

You can leave out either the :controller or :action parts of the render command if you're in the same controller or action that you want rendered.  render defaults to using the current controller and action.

So if you want to render this right from inside the print_records action you can just type:
render :layout => false

It's okay that the form that calls it might be in a different action/controller.  The only thing that matters to the render method is where it is being called from right then.

You're welcome for the help :-)  We all started at the beginning.

Re: problem with @content_for_layout

ok, thanks again,
i put the render(:layout => false)
in my controller and was sent to a blank page.
is this because i have partials in the view to that controller ?

thanks

Re: problem with @content_for_layout

is there a file named app/views/groups/print_records.rhtml ?
Try adding some simple text (like 'hi there' or anything) to this file and see if it shows up when you submit the form.

If it doesn't, post the content of your form, your print_records.rhtml file (and all partials it includes) and your groups controller so I can have a closer look.

Re: problem with @content_for_layout

ok, these are both in the group controller

    def select_for_reports
        @group = Group.find(params[:id])
        @sites = @group.sites
    end
   
       
    def run_reports
      #render(:layout => false)
      @group = Group.find(params[:group])
      if params[:site_ids].nil? || params[:site_ids].empty?
         flash[:error] = 'no sites selected for reports.'
         return redirect_to (:action => "select_for_reports",
                        :controller => "group",
                        :id => @group )                             
      end
      @sites_to_display = params[:site_ids]
      @sites = Array.new
      @sites = @sites_to_display.collect { |site| Site.find(site) }
      @start = params[:start]
      @start_year = @start["start(1i)"]
      @start_month = @start["start(2i)"]     
      @start_date = Time.local(@start_year,
                                    @start_month, 1).strftime("%Y-%m")
      @start_time = Time.local(@start_year,  @start_month, 1)
      @finish = params[:finish]
      @finish_year = @finish["finish(1i)"]
      @finish_month = @finish["finish(2i)"]     
      @finish_date = Time.local(@finish_year,
                    @finish_month,1 ).strftime("%Y-%m")
      @finish_time = Time.local(@finish_year,  @finish_month, 1 )

      if @finish_time < @start_time
          flash[:error] = 'finish date cannot preceed start date'
          return redirect_to (:action => "select_for_reports",
                        :controller => "group",
                        :id => @group )
      end


this is the select_for_reports.rhtml in /views/group/

<h2>select parameters for this report</h2>



<form action="/group/run_reports" id="run_reports" method="post" name="myform">
<fieldset>
<legend>sites</legend>
<input type="button" onclick="checkall('myform')" value="check all"/>
<table cellpadding="3" cellspacing="3">



<% @sites.each do |site| %>
    <tr>
    <td> <%= check_box_tag "site_ids[]", site.id  %> </td>
    <td> <%= site.site_name %>  </td>
    </tr>
<% end %>



</table>
</fieldset>

<fieldset>
<legend>report selection</legend>
<table cellpadding="3" cellspacing="3">

  <tr>
  <td>
  <%= check_box 'irr', 'irr', {'checked' => "checked"}, 'y', 'n'  %></td>
  </td>
  <td>graph monthly irrigation
  </td>
  </tr>
 
  <tr>
  <td>
  <%= check_box 'lpc', 'lpc', {'checked' => "checked"}, 'y', 'n'  %></td>
  </td>
  <td>list status changes
  </td>
  </tr>

  <tr>
  <td> start  </td>
  <td>
   
    <select name="start[start(2i)]">
        <option value="1" selected="selected">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
       
        <option value="8">August</option>
        <option value="9">September</option>
        <option value="10">October</option>
        <option value="11">November</option>
        <option value="12">December</option>
        </select>
        <select name="start[start(1i)]">
        <option value="2006" selected="selected">2006</option>
        <option value="2007">2007</option>
        <option value="2008">2008</option>       
        <option value="2009">2009</option>       
    </select>
 
   
  </td>
  </tr>
 
  <tr>
  <td> finish </td>
  <td>
    <%= date_select("finish", "finish",
                 :discard_day => true,
                :order => [:month, :year]) %>
  </td>
  </tr>
 
 
   
 
</table>

</fieldset>

<input type="hidden" name="group" value="<%= @group.id %>" >

<%= submit_tag("run report") %>

<%= end_form_tag %>


and this is the run_reports.rhtml in the /views/group/

<% if @include_graph == 'y' %>
        <table cellpadding="1" cellspacing="2" border="1">
            <% @sites.each do |site| %>
                <tr>
                    <td>
                     <%= render(:partial => 'report_table',
                                :controller => 'group',
                                :locals => {:site => site}) %>
                    </td>
                    <td>
                        <%= render (:partial => 'bar_graph',
                                :locals => { :site => site,
                                     :start => @start_date,
                                     :finish => @finish_date} )%>
                    </td>
                 </tr>
           <% end %>
       </table>   
<% end %>


<% if @include_status_changes == 'y' %>
    <% @sites.each do |site| %>
        <%= render(:partial => "change_list",
                :locals => {:site => site,
                :start => @start_date}) %>
    <% end %>
<% end %>


i hope this helps, i am sorry, i did not know how to turn the code tags on, hope this is readable. i need something like you have at hoopla with the syntax highlighting. [ryanb: I inserted the code tags for you]

thanks for everything

shawn

Last edited by ryanb (2007-01-12 12:46:34)

Re: problem with @content_for_layout

thanks, ryanb

Re: problem with @content_for_layout

So if I'm assuming correctly when you submit the form either the page shows up with the proper layout but no content in the middle or (if you use :layout => false) it shows up blank.

It looks like the run_reports action is cut short in the code above.  Is that the whole thing or is there more?  I don't see an end tag for the action.

If that is the whole action then I think one of your problems is that  @include_status_changes and @include_graph never get set - so you'll never have anything printed to the screen.

Re: problem with @content_for_layout

sorry, didn't notice that, there is a little more to run_reports
here it is

def run_reports
      #render(:layout => 'report')
      @group = Group.find(params[:group])
      if params[:site_ids].nil? || params[:site_ids].empty?
         flash[:error] = 'no sites selected for reports.'
         return redirect_to (:action => "select_for_reports",
                        :controller => "group",
                        :id => @group )                             
      end
      @sites_to_display = params[:site_ids]
      @sites = Array.new
      @sites = @sites_to_display.collect { |site| Site.find(site) }
      @start = params[:start]
      @start_year = @start["start(1i)"]
      @start_month = @start["start(2i)"]     
      @start_date = Time.local(@start_year,
                                    @start_month, 1).strftime("%Y-%m")
      @start_time = Time.local(@start_year,  @start_month, 1)
      @finish = params[:finish]
      @finish_year = @finish["finish(1i)"]
      @finish_month = @finish["finish(2i)"]     
      @finish_date = Time.local(@finish_year,
                    @finish_month,1 ).strftime("%Y-%m")
      @finish_time = Time.local(@finish_year,  @finish_month, 1 )

      if @finish_time < @start_time
          flash[:error] = 'finish date cannot preceed start date'
          return redirect_to (:action => "select_for_reports",
                        :controller => "group",
                        :id => @group )
      end
     
      # the following will either by y or n
      if ! params[:irr].nil?
          @include_graph = params[:irr][:irr]       
      end
      if ! params[:lpc].nil?
          @include_status_changes = params[:lpc][:lpc]
      end

      if @include_graph == 'n' and @include_status_changes == 'n'
          flash[:error] = 'no reports selected to run'
          return redirect_to (:action => "select_for_reports",
                        :controller => "group",
                        :id => @group )
      end
      @report_table_codes =  KeyWord.all_status_codes
      @report_months = report_months(@start_date, @finish_date)     
    end


ok, this is really strange to me. if i leave out the render layout => false
line, the reports pull up the way that they are supposed to in the application.rhtml layout. ok, so i want the side bar to dissapear for the printable reports so i put in the render false thing and it does pull a blank page. it also happens if i put say.... an rhtml file in /views/layouts/my_reports.rhtml and put render(:layout => 'my_reports') the same thing happens. But yes, everything does show up if i just use the application.rhtml

thanks again

Re: problem with @content_for_layout

what if you move the render :layout => false to the very end of the action?

Re: problem with @content_for_layout

IT WORKED !

moving my render statement to the end of the action worked !
thanks very much.