Reputation: 12663
In a Rails 3.2 app I have a coffeescript function that includes a POST to create an Asset with attached file (using Carrierwave)
link = url_to_file
parent = $('#dom_obj').data('id')
$.post( url, { remote_file_url: link, parent_id: parent } )
This gives the following log:
Started POST "/assets" for 127.0.0.1 at 2013-10-05 14:53:57 +0700
Processing by AssetsController#create as JS
Parameters: {"remote_file_url"=>"https://the/correct/path", "parent_id"=>"520"}
In the controller
@asset = Asset.new(params[:asset])
@asset.save
I have some other methods to create an Asset, they all work fine. The variables passed into this ajax call are also correct. But @asset.save is failing due to an error in the uploader which implies that the parent_id is not being set correctly.
As all the components of this work perfectly via other upload pathways, my only conclusion is that the jquery ajax call is incorrect, and I guess I'm not setting the parameters correctly (would params[:asset] in my controller correctly interpret the parameters logged above?).
How to pass parameters to a javascript post so that Rails will interpret them correctly? Any guidance much appreciated as I'm going round in circles!
Upvotes: 0
Views: 424
Reputation: 522
from the log you provided, there is no such thing called params[:asset] provided by your POST call. So, what would work is to do that in your controller
@asset = Asset.new(params)
@asset.save
or if this doesn't work you could try
@asset = Asset.new(:remote_file_url => params[:remote_file_url], :parent_id => params[:parent_id])
@asset.save
or another solution would be to not change your controller but change your form input names in your html to asset[remote_file_url]
and asset[parent_id]
Upvotes: 1