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 'Click me', :url => {:action => "array"}, :with =>

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 =[:appointment])
       flash[:notice] = "Availability saved"
       flash[:error] = "There was a problem creating the

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

This is the excerpt from the development.log:

Processing AppointmentsController#array (for at 2008-09-19
16:50:20) [POST]
  Session ID:
"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/
default field list: ["username", "has_photo"]
  e[4;35;1mUser Columns (0.004000)e[0m e[0mSHOW FIELDS FROM
  e[4;36;1mUser Load (0.001000)e[0m e[0;1mSELECT * FROM `users`
WHERE (`users`.`id` = 1) e[0m
  e[4;35;1mAppointment Columns (0.004000)e[0m e[0mSHOW FIELDS FROM
  e[4;36;1mSQL (0.000000)e[0m e[0;1mBEGINe[0m
  e[4;35;1mSQL (0.000000)e[0m e[0mCOMMITe[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:

"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:

"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.



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 %>


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.

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

  @appointment =[:appointment])

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?



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