Topic: Need Help ASAP: Generate a link_to_remote tag....

Hello!

I am doing some AJAX work in one of my pages.

What I am trying to do is use a form to add elements to a table and a live database via AJAX. Now, I can do that, using form_remote_tag with no issues at all - it calls a method in my controller which adds the record from the form to the database, and then inserts a row into an onscreen table.

Works great.

Now, what I want to do is a touch more difficult. I want to include a link_to_remote on each row I generate which will

1. Call a method to delete the record in the database
2. Use Element.remove to remove the corresponding row in the table.

Now, when I add an element to the database/table via the form, it calls this method from my controller:

def add_item
  item = Item.new(@params['item'])
  render_text "<tr id=item" + item.id.to_s + " >" +
                  "<td>" + item.quantity.to_s + "</td>" +
                  "<td>" + item.model.description + "</td>" +
                  "<td>" + item.colour.title + "</td>" +
                  "<td>" + item.numshelves.to_s + "</td>" +
              "</tr>"
end

this creates the item, and adds it to the database and the visible table. Now, in order to add the link to remote, I suppose I have to do something like:

def add_item
  item = Item.new(@params['item'])
  render_text "<tr id=item" + item.id.to_s + " >" +
                  "<td>" + item.quantity.to_s + "</td>" +
                  "<td>" + item.model.description + "</td>" +
                  "<td>" + item.colour.title + "</td>" +
                  "<td>" + item.numshelves.to_s + "</td>" +
                  "<td>" + link_to_remote("(delete)",
                          :url => { :action => "deleteitem", :id => item.id },
                           :success => "<script>Element.remove('item' + item.id.to_s)</script>") +
               "</tr>"
end

This doesn't work, however - syntax errors. I don't even know if this is the correct way to accomplish this. How should I accomplish this? What do you suggest?

Last edited by Caydel (2006-07-25 12:07:09)

Check out my Blog.

Re: Need Help ASAP: Generate a link_to_remote tag....

RJS is really the way to go when doing AJAX in Rails. If you are willing to spend a few dollars, get the RJS book. There are some tutorials out there, but nothing covers it better than the book from what I've seen.

As for your specific problem, the syntax error is probably caused from the controller trying to call a view helper method (link_to_remote). For a quick fix, try placing the row into a partial and rendering that.

def add_item
  @item = Item.new(params[:item])
  render :partial => 'item_row'
end

BTW, the item isn't getting saved to the database in that method. You need to call "item.save" for that.

Last edited by ryanb (2006-07-25 12:31:09)

Railscasts - Free Ruby on Rails Screencasts

Re: Need Help ASAP: Generate a link_to_remote tag....

That did it!

I think I had better but a copy of the book...

Check out my Blog.

Re: Need Help ASAP: Generate a link_to_remote tag....

ryanb wrote:

RJS is really the way to go when doing AJAX in Rails. If you are willing to spend a few dollars, get the RJS book. There are some tutorials out there, but nothing covers it better than the book from what I've seen.

As for your specific problem, the syntax error is probably caused from the controller trying to call a view helper method (link_to_remote). For a quick fix, try placing the row into a partial and rendering that.

def add_item
  @item = Item.new(params[:item])
  render :partial => 'item_row'
end

BTW, the item isn't getting saved to the database in that method. You need to call "item.save" for that.

The book you mentioned is great - I would recommend it to anyone who is planning on using AJAX with their Ruby. RJS templates are one of the most impressive features of Ruby.

Check out my Blog.

Re: Need Help ASAP: Generate a link_to_remote tag....

Caydel wrote:

The book you mentioned is great - I would recommend it to anyone who is planning on using AJAX with their Ruby. RJS templates are one of the most impressive features of Ruby.

Wow, and you've had less than an hour and a half with it. Being able to learn something so quickly is certainly a testament to Ruby, Rails, RJS and You. smile

Railscasts - Free Ruby on Rails Screencasts

Re: Need Help ASAP: Generate a link_to_remote tag....

ryanb wrote:
Caydel wrote:

The book you mentioned is great - I would recommend it to anyone who is planning on using AJAX with their Ruby. RJS templates are one of the most impressive features of Ruby.

Wow, and you've had less than an hour and a half with it. Being able to learn something so quickly is certainly a testament to Ruby, Rails, RJS and You. smile

I can't exactly claim that I know it well, but it is only a 50 page book, large type.

Ruby is not a tough language. Rails makes it simpler, and RJS makes certain applications of Ruby on Rails immensely simpler yet.

Check out my Blog.