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
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
David
Sent: Friday, September 19, 2008 4:52 PM
To: Ruby on Rails: Talk
Subject: [Rails] Re: Saving to the database using link_to_remote?
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
On Sep 19, 3:49 pm, "Pardee, Roy" <[EMAIL PROTECTED]> wrote:
> 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?
>
> -----Original Message-----
> From: [email protected]
> [mailto:[EMAIL PROTECTED] On Behalf Of David
> Sent: Friday, September 19, 2008 3:19 PM
> To: Ruby on Rails: Talk
> Subject: [Rails] 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---