Forum: Ruby on Rails Re-rendering layout with form_remote_tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jason (Guest)
on 2007-02-21 17:27
I have a calendar that I'm trying to update via ajax:  A user selects a
different month, clicks the submit button, and the calendar shows the
month they selected without reloading the whole page.

Now, my problem is that it is rendering the layout multiple times.  I'm
getting cascading menus, images, etc.

Here's my controller code:

def dyna_calendar
    #get either current month or requested month
  if not request.post?
    @selected_date = Date.today
    @year = Time.now.year
    @month = Time.now.month
  else
    @selected_date = Date.new(params[:date][:year].to_i,
params[:date][:month].to_i, Time.now.mday)
    @year = Date.new(params[:date][:year].to_i, Time.now.month,
Time.now.mday).year
    @month = Date.new(Time.now.year,params[:date][:month].to_i,
Time.now.mday).month
  end

  @sales_orders = SalesOrder.find(:all,
                                    :conditions => ['month(ship_date) =
? and  year(ship_date) = ?',@month,@year])

  render :partial => 'calendar_header', :layout => 'standard'

end

And here is the content of _calendar_header:

<div id='ajax_cal'>
<h2>Ship Date Calendar</h2>

<table class="list_header"><tr><td>
<% form_remote_tag(:update => 'ajax_cal', :url => {:action =>
'dyna_calendar'}) do -%>
    <%= select_month(@selected_date)%>
    <%= select_year(@selected_date)%>
    <%= submit_tag 'Go' %>
<% end -%></td>
<td>
    <%= '<b>' + @sales_orders.length.to_s + '</b> total orders this
month. &nbsp; Total: <b>' + sprintf("%#1.2f",
@sales_orders.sum(&:amount)) + '</b>'%>
</td>
</tr></table>


<%=
calendar({:year => @year, :month => @month, :abbrev => (0..0)}) do |d|
  cell_text = "#{d.mday}<br />"
  cell_attrs = {:class => 'day'}

  @sales_orders.each do |o|
    if (d.mday == Time.now.mday and d.month == Time.now.month)
        cell_attrs[:style] = 'background: #ff9;'
    end
    if (o.ship_date.month == d.month and o.ship_date.mday == d.mday )
          cell_text << link_to( o.id.to_s, :controller =>
'sales_orders', :action => 'show',
                                :id => o ) << '<br /><span
class="cal_text">' +truncate(o.sold_to, 15) + '<br />' +
truncate(o.description,12) + '<br />' + sprintf("%#1.2f", o.amount) +
"</span><br /><br />"
          cell_attrs[:class] = 'specialDay'
    end
  end
  [cell_text, cell_attrs]
end
%>
</div>

How can I render the layout only once, no matter how many times the user
changes the calendar month?  I'm sure I'm missing something easy here.
Thanks!
Jodi S. (Guest)
on 2007-02-21 17:56
(Received via mailing list)
On 21-Feb-07, at 10:27 AM, Jason wrote:

> Here's my controller code:
>     @year = Date.new(params[:date][:year].to_i, Time.now.month,
>   render :partial => 'calendar_header', :layout => 'standard'
>
> end
>

you can render :layout => false

> How can I render the layout only once, no matter how many times the
> user
> changes the calendar month?  I'm sure I'm missing something easy here.
> Thanks!


does that help?
J
Jason (Guest)
on 2007-02-21 18:14
> you can render :layout => false


Yes, that did it.

Thanks so much!
This topic is locked and can not be replied to.