Forum: Ruby on Rails Help with link_to_remote and RJS

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.
006a4831843b48f6102d5d3cf92ae283?d=identicon&s=25 Shilo Ayalon (gte351s)
on 2009-01-14 11:03
Hi all -

New to rails, I have a page that contains a simple table for vendors,
with name a location fields. I'm trying to create an 'add vendor' link,
that adds a small to insert a new vendor on the fly. Here's what I have
in code:

# app/views/vendors/index.html.erb
<h1>Our Vendors</h1>
<div id="test_div"></div>
<%= link_to_remote 'add vendor', :url => { :action => 'new' } %>
<table>
  <tbody id="vendors-table">
    <tr id="vendors-headers">
      <th>Vendor</th>
      <th>Location</th>
    </tr>
    <%= render :partial => 'vendor', :collection => @vendors %>
  </tbody>
</table>
# app/views/vendors/_vendor.html.erb
<% @vendor = vendor %>
<tr id="vendor_<%= vendor.id %>">
  <td><%= vendor.name %></td>
  <td><%= vendor.location %></td>
</tr>
# app/views/_new_vendor.html.erb
<tr>
  <% form_for :vendor, :url => { :action => 'create' } do |f| -%>
    <td><%= f.text_field :name, :size => 15 %></td>
    <td><%= f.text_field :location, :size => 15 %></td>
    <td><%= submit_tag 'save' %></td>
  <% end -%>
</tr>
# app/controllers/vendors_controller.rb
class VendorsController < ApplicationController

  def index
    @vendors = Vendor.find(:all, :order => 'name')
  end

  def new
    @vendor = Vendor.new
    respond_to do |format|
      format.html { render :partial => 'new_vendor', :object =>
Vendor.new }
      format.js
    end
  end

  def create
    @vendor = Vendor.new(params[:vendor])
  end
end
# app/views/vendors/new.js.rjs
page.insert_html :after, 'vendors-headers', :partial => 'new_vendor',
:object => Vendor.new
I have a couple of issues. The first is that clicking the 'add vendor'
does add a table row with the form to the markup, but it doesn't show on
the page. I can see the page source adding the rows with the form
elements (in firebug), but it's as if the "display:none" is set on them
(no css is linked to the page, so that isn't it). I'm thinking it might
be because I'm not linking the form to the model properly, but I can't
see where I might be erring.

The other thing is I'm not sure how to complete the 'create' part so
that the new vendor is added properly to the table via rjs.

Any help will be awesome!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-14 12:14
(Received via mailing list)
On 14 Jan 2009, at 10:03, Shilo Ayalon wrote:
> them
> (no css is linked to the page, so that isn't it). I'm thinking it
> might
> be because I'm not linking the form to the model properly, but I can't
> see where I might be erring.
>
I don't think it's not legal markup. a <tr> tag may only contain <td>
tags.

Fred
This topic is locked and can not be replied to.