I've seen different questions with the same issue but none did fix my error.
I'm getting 406 Unknown format when posting a new campaign
record with a form and axios, and can't figure why since I'm specifying the json format in the controller:
Errors
spread.js:25 POST http://localhost:4000/campaigns 406 (Not Acceptable)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Action Controller: Exception caught</title>
<header>
<h1>
ActionController::UnknownFormat
in CampaignsController#create
</h1>
</header>
Routes file
resources :campaigns
Campaigns controller
def create
@campaign = Campaign.new(campaign_params)
if current_user
@campaign.user_id = current_user.id
end
respond_to do |format|
format.json do
if @campaign.save
format.json { render :show, status: :ok, location: @campaign }
else
format.json { render json: @campaign.errors, status: :unprocessable_entity }
end
end
end
end
Form
methods: {
submit() {
var data = new FormData()
data.append('box', this.campaign.selectedBox.id)
for (const product of this.campaign.selectedProducts) {
data.append('products[]', product.id)
}
data.append('image', this.campaign.photo1)
data.append('campaign', this.campaign.id)
axios.interceptors.request.use(config => {
config.paramsSerializer = params =>
qs.stringify(params, {
arrayFormat: 'brackets',
encode: false
});
return config
})
axios.post('/campaigns', data, {
headers: {
'Content-Type': 'application/json',
}
}).then((response) => {
location.href = response.data.url
}).catch((error) => {
console.log(error.response.data)
})
},
¿Am I missing something? Thanks in advance.