# Saving to the database using link\_to\_remote?

**URL:** <https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071>\
**Category:** Rails\
**Created:** [September 20, 2008, 12:19am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071 "2008-09-20T00:19:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 12:19am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/1 "2008-09-20T00:19:35Z")

</div>

I am attempting to save a javascript array that has collected clicks  
from the user into the database using link\_to\_remote. I am new to  
ajax, so I do not know all the details but I have been told that in  
order to get the javascript array from the client side to the server  
side I must make an ajax request. The ajax helper I am using is  
link\_to\_remote:

\<%= link\_to\_remote ‘Click me’, :url =\> {:action =\> “array”}, :with =\>  
“‘data=’+test()”%\>

This works up until trying to save the array to the database. It  
passes the array, which is the result of the javascript function  
test() to the action array and I am able to convert to ruby. However,  
I am unable to save anything to the database, or redirect to any other  
action. I am wondering, firstly, Is link\_to\_remote even the  
appropriate ajax helper, and if so, How can I save this information to  
the database in the array action as seen above? Thanks, Dave

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 12:50am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/2 "2008-09-20T00:50:05Z")

</div>

I think if you want to redirect, you need to do a plain request–that  
is, drop the ajax & just do link\_to.

But you should be able to save to the db from an ajax request. What are  
you doing to save to the db & how do you know it’s not working? If  
there’s an error, what is it?

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 1:52am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/3 "2008-09-20T01:52:40Z")

</div>

The problem is that I don’t think I can pass the javascript array with  
just link\_to, I think it needs to be a javascript request. This is  
the call I am using to save to the database:

@appointment = logged\_in\_user.appointments.build(params[:appointment])  
if @appointment.save  
flash[:notice] = “Availability saved”  
else  
flash[:error] = “There was a problem creating the  
appointment.”  
end

where params[:appointment] is the appropriate hash. I know it is not  
saving b/c it displays the error and nothing is changed in the actual  
database.

This is the excerpt from the development.log:

Processing AppointmentsController#array (for 127.0.0.1 at 2008-09-19  
16:50:20) [POST]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlZWZjMGJlN2RkN2Y0YmNkNWM1OGJkOTUyMjA2MjJl  
%0AZjkiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh  
%0Ac2h7BjoLbm90aWNlMAY6CkB1c2VkewY7B1Q6CXVzZXJpBg%3D  
%3D–364263bf2727a15d868a5e7438b049fccd17c21d  
Parameters:  
{“authenticity\_token”=\>“d6f4591942661666d16d0a11e53b1436f9dca797”,  
“action”=\>“array”, “controller”=\>“appointments”, “data”=\>“10”}  
Asked for a remote server ? true, ENV[“FERRET\_USE\_LOCAL\_INDEX”] is  
nil, looks like we are not the server  
Will use local index.  
using index in C:/InstantRails-2.0-win/rails\_apps/clean/index/  
development/user  
default field list: [“username”, “has\_photo”]  
[4;35;1mUser Columns (0.004000) [0m [0mSHOW FIELDS FROM  
`users` [0m  
[4;36;1mUser Load (0.001000) [0m [0;1mSELECT \* FROM `users`  
WHERE (`users`.`id` = 1) [0m  
[4;35;1mAppointment Columns (0.004000) [0m [0mSHOW FIELDS FROM  
`appointments` [0m  
[4;36;1mSQL (0.000000) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.000000) [0m [0mCOMMIT [0m  
Rendering appointments/array  
Completed in 0.07400 (13 reqs/sec) | Rendering: 0.00700 (9%) | DB:  
0.00900 (12%) | 200 OK [[http://localhost/appointments/array](http://localhost/appointments/array)]

Thanks, Dave

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 2:20am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/4 "2008-09-20T02:20:12Z")

</div>

Hrmm–out of my depth here probably… But it looks like you’re not  
getting an appointments hash in your params at all:

{“authenticity\_token”=\>“d6f4591942661666d16d0a11e53b1436f9dca797”,  
“action”=\>“array”, “controller”=\>“appointments”, “data”=\>“10”}

You’re just getting a simple param called “data”–so if you said  
“params[:data]” in your controller code you should get back a “10”. But  
this is nothing you’re going to be able to .build a whole appointment  
out of. You want that parameters hash to come back looking someething  
like this:

{“authenticity\_token”=\>“d6f4591942661666d16d0a11e53b1436f9dca797”,  
“action”=\>“array”, “controller”=\>“appointments”,  
“appointment”=\>{“field\_one”=\>“some value”, “field\_two”=\>“some other  
value”}, “data”=\>“10”}

I’m guessing you’ve got a whole form here for creating an appointment,  
and you want all your inputs, plus this js-generated array of clicks?  
If that’s it, and you really need to do ajax, have a look at  
remote\_form\_for. That’s an ajaxy form-submitter. But even then you’re  
going to have to do something special w/your “data” parameter–it’s not  
going to come in as part of the appointment hash the way you’re passing  
it now.

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 2:51am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/5 "2008-09-20T02:51:11Z")

</div>

Actually, upon re-reading your post, I think you are misunderstanding  
what I am trying to do. I am only passing the array through ajax,  
then performing a number of functions on the array data, and then  
trying to save it. I don’t need any more information from the user  
than the data in the array.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 2:47am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/6 "2008-09-20T02:47:40Z")

</div>

Yea, remote\_form\_for seems like it might work, but Im just not sure  
how to pass the javascript array with this. Anyone have any idea?  
This is what Im trying but it doesnt seem to work:

```
<% remote_form_for :appointment, @appointment, :url => {:action =>

```

“array”}, :with =\> “‘data=’+test()” do |f|%\>  
\<%= submit\_tag(“submit”) %\>  
\<% end %\>

thanks

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 3:04am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/7 "2008-09-20T03:04:38Z")

</div>

Hmmm–is this not the controller code your ltr call is posting to?

@appointment = logged\_in\_user.appointments.build(params[:appointment])  
if @appointment.save  
\<\<etc.\>\>

That looks like you’re trying to create a new appointment for the  
current user. If you’re looking to save the contents of params[:test]  
then shouldn’t you be doing something w/that?

If that’s not the controller code you’re posting to, can you show that?  
I’m guessing it’s the ‘array’ method in your appointments\_controller.rb?

Cheers,

-Roy

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 20, 2008, 5:03am UTC](https://www.ruby-forum.com/t/saving-to-the-database-using-link-to-remote/149071/8 "2008-09-20T05:03:33Z")

</div>

Hey thanks fo helping but I think I figured it out. I used .save!  
instead of just .save and it seems to be working.
