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|
%>
<%= car.name %> |
<%= hidden_field_tag :car_id, car.id %>
<% car.group.checks.each do |c| %>
<%= text_field_tag 'check_' + c.id.to_s, car.check_at
(c.id), :size => 1 %> |
<% end %>
<%= f.submit 'Record' %> |
<% 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
On Fri, Oct 23, 2009 at 4:46 PM, Alex2101 [email protected] 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 ~
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. [email protected]
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.
2009/10/24 Alexandre P. [email protected]:
  Â
    <%= 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