Saving to the database using link_to_remote?

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

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?

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]

Thanks, Dave

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

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.

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

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

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