I have the following action to create a record:
def create
@categorization = Categorization.new(categorization_params)
@categorization.save
respond_to do |format|
format.json { render json: {categorization: @categorization.to_json}, success: 200 }
end
end
private
def categorization_params
params.require(:categorization).permit(:user_id, :category_id)
end
and this is my jquery ajax code:
$("#save-category").click(function(){
var category_id = $('#categorization_category_id').val();
var category_name = $('#categorization_category_id option:selected').text();
var user_id = "#{current_user.id}";
var cat_id;
$.ajax({
url: "#{dashboard_categorizations_path}",
type: 'POST',
dataType : 'json',
data: {'user_id': user_id, 'category_id': category_id},
success: function(result) {
console.log(result);
// cat_id = JSON.parse(result.id);
// alert(cat_id);
},
error: function(result) {
console.log(result);
// cat_id = JSON.parse(result.id);
// alert(cat_id);
}
}); // end ajax call
$("#categoriamensaje").show();
var leadMessage = setInterval(function(){
$("#categoriamensaje").hide();
}, 3000);
});
This is what my inspector show me:
As you can see, after make click on the button I get to responds I with status 400 and one with status 200:
The first one give me the following response:
ActionController::ParameterMissing at /dashboard/categorizations
param is missing or the value is empty: categorization
app/controllers/dashboard/categorizations_controller.rb, line 30
``` ruby 25 redirect_to edit_dashboard_home_path(current_user.id) 26 end 27 28 private 29 def categorization_params
30 params.require(:categorization).permit(:user_id, :category_id) 31 end 32 end
```
and the second one:
categorization:"{"id":60,"user_id":1,"category_id":7,"created_at":"2015-04-15T14:27:55.039Z","updated_at":"2015-04-15T14:27:55.039Z"}"
The record is created, but I need to catch the ajax response in order to show the information in the view.
Some help please.