'Rails Remote True Ajax ActionController::UnknownFormat
I have a mini-update method in my model that only updates one field, set up in a chart (so I see the form for each row). It works properly, but want it to update using AJAX to be able to do multiple updates quickly without waiting for a full page reload.
The cell looks like this:
<td style="min-width: 150px" id="goody_cell<%= blog.id %>">
<%= render partial: "admin/goody_cell", locals: { blog: blog } %>
</td>
With this as the partial:
<% if blog.variation_id %>
<%= Variation.find(blog.variation_id).name %>
<% end %>
<%= simple_form_for(blog, url: update_goody_path(blog), remote: true, method: :patch, authenticity_token: true) do |f| %>
<% variation_collection = [] %>
<% Variation.all.each do |lm| %>
<% variation_collection << ["#{lm.name}", "#{lm.id}"] %>
<% end %>
<div class="row text-left">
<div class="form-group col-12 mb-0 pb-0">
<%= f.input :variation_id, label: false, prompt: "Choose a Related Goody", input_html: { id: "lmFor" + blog.id.to_s, class: 'browser-default', onchange: "this.form.submit();" }, collection: variation_collection, required: false %>
</div>
</div> <!-- row -->
<% end %>
My blogs/update_goody.js.erb
looks like this:
$('#goody_cell<%= blog.id %>').html('<%= j render partial: "admin/goody_cell", locals: { blog: @blog } %>');
And my controller method is like this:
def update_goody
@blog = Blog.friendly.find(params[:id])
if @blog.update!(blog_params)
respond_to do |format|
format.js
end
else
format.html { render :edit }
format.json { render json: @blog.errors, status: :unprocessable_entity }
end
end
I have the route like this:
patch "blogs/:id/update_goody" => "blogs#update_goody", as: "update_goody"
When I try to update it, it does update the value, but before it renders the partial, I get an error saying:
ActionController::UnknownFormat - ActionController::UnknownFormat:
app/controllers/blogs_controller.rb:178:in `update_goody'
I've looked at SO posts like this but they all say that the error was fixed by adding remote: true
, which I already have.
Can anyone help me get this working?
Solution 1:[1]
make sure you have both update_goody.html.erb
and update_goody.js.erb
in your views/blogs
folder.
you dont have to add anything to update_goody.html.erb
as its not rendering at all.
in your controller
def update_goody
@blog = Blog.friendly.find(params[:id])
if @blog.update!(blog_params)
respond_to do |format|
format.html
format.js
end
else
format.html { render :edit }
format.json { render json: @blog.errors, status: :unprocessable_entity }
end
end
note that format.html
in respond_to
block. it dosent matter you add it or not as you are set remote: true
in your form
refer this
Solution 2:[2]
instead of remote: true
, use local: false
(Rails 6)
Solution 3:[3]
In my case i had to put redirect_to
to render html
respond_to do |format|
format.js
format.html { redirect_to update_goody_path }
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | roshiend |
Solution 2 | Aneesh Patel |
Solution 3 | Rick Max |