SD1990
SD1990

Reputation: 808

Setting a href of a button in Ruby/Javascript

This question is hard to explain so please bear with me. I have a table that contains rows which hold records from the Records table. i can add rows to this table using ajax and when i have entered the hours into a text field on the row and click away, the record gets submitted via ajax. My problem is that i have a delete button next to each row. The rows delete as youd expect when i have refreshed the page or the rows where already in the Records table when the page was loaded. However if i have just added some hours and havent refreshed the page then i cannot delete the record.

After searching ive narrowed it down to this:

Already present record:

<a rel="nofollow" data-method="delete" data-confirm="Are you sure?" href="/records/2">

Just added:

<a class="delete" title="delete row" rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure?" href="Delete">

Because im using the ruby method "delete" from the Records controller it needs a href with the record id, thus "records/2". this wont change until ive refreshed the page which is not what i want.

Ive thought about setting the href of the button when the record gets submitted but i have know idea how to do that in either js or ruby, plus id have to submit and then grab the record id back again. Or if someone can come up with a better idea other than the one above and "refresh the page when it submits" im all ears.

Upvotes: 1

Views: 157

Answers (1)

djd
djd

Reputation: 1001

Iam guessing you are using rails so what u need to do is the controller should return a js response. check this question. to render erb code in JavaScript you should use escape_javascript.

Upvotes: 2

Related Questions