Forum: Ruby on Rails Ajax table does not update

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.
164bad3bfc412e3d0442d199a3780eff?d=identicon&s=25 oom tuinstoel (Guest)
on 2006-06-04 13:24
I have two models: Child and Day. A Child has many days, and a day
belongs to a child.

I want to create ana ajax table listing the children and, after pressing
a button, I want to show the days that belong to that child.

Here is my code:

(list.rhtml)
<table>
  <tr>
    ...
    <td><%= link_to 'Days', :action => 'show_days', :id => child %></td>
  </tr>
  <tr>
    <div id="show_days" style="display: none"></div>
  </tr>
</table>

(child_controller
  def show_days
    @child = Child.find(params[:id])
    @days = @child.days
  end

(child.chow_days.rjs)
page.replace_html 'show_days', :partial => 'days', :collection => @days
page.visual_effect :toggle_appear, 'show_days', 'duration' => 0.5

(_days.rhtml)
<% for day in @days -%>
  <td><%=h day.startdate %></td>
  <td><%=h day.enddate %></td>
<% end -%>

The problem is, in Firefox I get a new page with the following text:

try {
Element.update("show_days", "  <td>2005-09-01</td>\n
<td>2006-07-01</td>\n");
Effect.toggle("show_days",'appear',{duration:0.5});
} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('Element.update(\"show_days\", \"  <td>2005-09-01</td>\n
<td>2006-07-01</td>\n\");\nEffect.toggle(\"show_days\",\'appear\',{duration:0.5});');
throw e }

What am I doing wrong?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-04 20:25
oom tuinstoel wrote:
>     <td><%= link_to 'Days', :action => 'show_days', :id => child %></td>

You're not using ajax, just a standard link_to.  So rails renders the
javascript and sends it with text/html headers and is displayed as text
by the browser on a new page.

You must start the ajax request by using link_to_remote:

  <%= link_to_remote 'Days', :url => {:action => 'show_days', :id =>
child} %>
164bad3bfc412e3d0442d199a3780eff?d=identicon&s=25 oom tuinstoel (Guest)
on 2006-06-05 13:03
>Alex Wayne wrote:
>
>   <%= link_to_remote 'Days', :url => {:action => 'show_days', :id =>
> child} %>

Yes Ales, you are absolutely right. But... it is still not working! Is
there something else I am overlooking????
164bad3bfc412e3d0442d199a3780eff?d=identicon&s=25 oom tuinstoel (Guest)
on 2006-06-05 19:35
IT WORKS!

Yes, I found it myself! Here's how I did it:

<table>
  <tr>
    <th>...</th>
  </tr>

 <% for child in @children %>
      <tr>
        <td>..</td>
      </tr>
      <tr>
        <td colspan="9"><div id='days' style="display: none"></td>
      </tr>
  <% end %>
</table>

The "colspan=9" did the trick.

Hope I can save somebody else some time with this post!
This topic is locked and can not be replied to.