My task is to upload an image and update the user table without the page being refreshed. My search suggested to grab the form elements with refs
but I'm getting, in console:
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/users_controller.rb:8:in
user_params' app/controllers/users_controller.rb:3:in
update'
Routes:
Rails.application.routes.draw do
get 'welcome/index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'welcome#index'
resources :users do
member do
post 'upload' => 'users#update'
end
end
end
The app:
var User = React.createClass({
componentDidMount: function(){
// Trying so see what this shows in the browser console
var foo = $(this.refs.myForm).elements;
console.log(foo);
}.bind(this),
onUpload: function(){
var formData = $(this.refs.myForm).serialize();
$.ajax({
url: '/users/1/upload',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
console.log("success: " + data);
},
error: function () {
alert("error in ajax form submission");
}
});
},
render: function(){
return(
<div>
<form ref="myForm" remote="true">
<input type="file" name="user[avatar]" />
</form>
{/* Placing the button outside the form prevents a refresh */}
<button className="button" onClick={this.onUpload}>Upload</button>
</div>
)
}
});
module.exports = User;
User Controller:
class UsersController < ApplicationController
def update
User.find(1).update_attributes(user_params)
end
private
def user_params
params.require(:user).permit(:avatar)
end
end
I've see that, on the web, other people used this.refs.myForm.elements
so what's wrong here?