# Render or update only one element of a table

**URL:** <https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784>\
**Category:** Rails\
**Created:** [October 23, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784 "2009-10-23T17:32:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex2101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@Alex2101](https://www.ruby-forum.com/u/Alex2101)\
**Post date:** [October 23, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784/1 "2009-10-23T17:32:39Z")

</div>

Hi,

I am pretty new to ror…  
I create a table filled thanks to a partial with a collection.  
Each line is a “form\_for” ending with a submit which update some  
values for a specific row.  
My problem is that I would like, after clicking on the submit button  
that, the concerned row and only this one updates and not the whole  
table. With the following code, it update well, but at the end at only  
view the modified row (loosing application layout, table view…)

Can someone help me ?

I have the following views :

--------- views/home/index.html.erb

\<% content\_for :content do %\>

# \<%= @group.name %\>

```
   <!-- Headers -->
  <tr>
    <th>Cars</th>
    <% @group.checks.each do |c| %>
      <th><%= c.name %></th>
    <% end %>
  </tr>

  <!-- Values -->
  <%= render :partial => "cars", :collection => @group.cars%>
</table>

<%= link_to 'Add car', new_car_path %>

```

\<% end %\>

--------- views/home/\_car.html.erb

 \<% form\_for car, :url =\> { :action =\> "update\_car\_checks" } do |f| %\> \<%= hidden\_field\_tag :car\_id, car.id %\> \<% car.group.checks.each do |c| %\> \<% end %\> \<% end %\>

Then, in home controller :  
--------- controllers/home\_controller.rb  
def update\_car\_checks  
@car = Car.find(params[:car\_id])

#… Some updates and check for the car…

render :partial =\> “car”  
end

| \<%= car.name %\> | \<%= text\_field\_tag 'check\_' + c.id.to\_s, car.check\_at (c.id), :size =\> 1 %\> | \<%= f.submit 'Record' %\> |

---

<div class="post-metadata">

**Author:** ![Alex2101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@Alex2101](https://www.ruby-forum.com/u/Alex2101)\
**Post date:** [October 23, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784/2 "2009-10-23T17:42:02Z")

</div>

On Fri, Oct 23, 2009 at 4:46 PM, Alex2101 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> view the modified row (loosing application layout, table view…)
> 
> Can someone help me ?

You should submit that row using AJAX, and get the result to update that  
row.  
You have a few possible ways to do this. you should check the  
button\_to\_remote doc and you Javascript framework doc to implement a  
unobstrusive solution.  
Basically, the procedure is that, you submit your fields, via AJAX,  
and update the right row with the result.

> ```
> <th>Cars</th>
> 
> ```
> 
> \<% end %\>  
> \<% end %\>
> 
> render :partial =\> “car”  
> end
> 
> >

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![Alex2101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@Alex2101](https://www.ruby-forum.com/u/Alex2101)\
**Post date:** [October 24, 2009, 6:26pm UTC](https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784/3 "2009-10-24T18:26:27Z")

</div>

Thanks, you send me on the right way. This is how I resolve it thanks to  
Ajax :

--------- views/home/\_car.html.erb

 \> \<% remote\_form\_for car do |f| %\> \<%= car.name %\>

```
<% car.group.checks.each do |c| %>
  <td><%= text_field_tag 'check_' + c.id.to_s, car.check_at(c.id), 

```

:size  
=\> 1 %\>  
\<% end %\>

```
<td>
  <%= submit_to_remote 'update_btn', 'Update',
    :url => { :action => 'update_car_checks', :id => car.id },
    :update => 'car_' + car.id.to_s
  %>
</td>

```

\<%end%\>

And that’s all !

2009/10/23 Leonardo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > that, the concerned row and only this one updates and not the whole  
> > Basically, the procedure is that, you submit your fields, via AJAX,
> > 
> > ```
> > <td><%= text_field_tag 'check_' + c.id.to_s, car.check_at
> > 
> > ```
> 
> –  
> Leonardo M…  
> There’s no place like ~
> 
> >

–  
Cordialement,  
Alexandre P.

---

<div class="post-metadata">

**Author:** ![Alex2101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@Alex2101](https://www.ruby-forum.com/u/Alex2101)\
**Post date:** [October 25, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/render-or-update-only-one-element-of-a-table/177784/4 "2009-10-25T21:08:09Z")

</div>

2009/10/24 Alexandre P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â Â   
> Â Â Â Â \<%= submit\_to\_remote ‘update\_btn’, ‘Update’,  
> Â Â Â Â Â :url =\> { :action =\> ‘update\_car\_checks’, :id =\> car.id },  
> Â Â Â Â Â :update =\> ‘car\_’ + car.id.to\_s  
> Â Â Â Â %\>  
> Â Â Â   
> Â Â \<%end%\>

I think a form tag is not allowed to enclose a row, it must include  
the whole table or fit inside a cell. Copy the generated html and  
paste it into the w3c html validator web page to check.

Colin
