Reputation: 145
I'm having trouble with editing an object from the admin panel on my app. (It's a political related app, hence the references to Liberal and Conservative, etc).
When I edit a category it creates a new category (with the same name) instead of updating it. I can't figure out why.
I can see from the log that when I edit the category Rails appears to go to the POST route (POST /admin/categories(.:format) admin/categories#create)
instead of the PUT or PATCH route (PATCH /admin/categories/:id(.:format) admin/categories#update)
Apologies if I am using incorrect terminology, I'm not well versed in Rails.
categories_controller.rb:
class Admin::CategoriesController < Admin::DashboardController
before_action :find_category, only: [ :edit, :update, :destroy ]
def index
@categories = Category.all
end
def new
@category = Category.new
end
def create
@category = Category.new(category_params)
if @category.save
flash[:notice] = 'Category created'
redirect_to admin_categories_path
else
flash[:error] = 'Something was wrong'
render :new
end
end
def edit
end
def update
@category.update(category_params)
if @article.save
flash[:notice] = 'Category saved'
redirect_to admin_categories_path
else
flash[:error] = 'Something was wrong'
render :edit
end
end
def destroy
@category.destroy
redirect_to admin_categories_path
end
private
def find_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :liberal_image, :conservative_image)
end
end
edit.html.slim inside the admin view (using slim):
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
.container
= link_to 'Manage Categories', admin_categories_path
h1 = "Edit Category - #{@category.name}"
= render partial: 'form', locals: { category: @category }
= simple_form_for @category, url: admin_category_path, method: 'delete' do |f|
= f.submit 'Delete category', class: 'btn btn-danger btn-lg'
I'm presuming it's something wrong in one of these files but let me know if there's something else I need to copy up there. Thanks !
_form.html.slim
= simple_form_for @category, url: admin_categories_path, method: 'post' do |f|
div.form-group.required
= f.input :name, label: 'Category Name'
div.form-group.required
= f.input :liberal_image, as: :file, label: 'Liberal Image'
div.target
div.form-group.required
= f.input :conservative_image, as: :file, label: 'Conservative Image'
div.target
div.form-actions
= f.submit 'Save', class: 'btn'
javascript:
$(document).ready(function() {
$('input[type="file"]').on('change', function(event) {
var files = event.target.files;
var image = files[0]
var reader = new FileReader();
reader.onload = function(file) {
var img = new Image();
img.src = file.target.result;
$(event.target).parent().parent().find('.target').html(img);
}
reader.readAsDataURL(image);
console.log(files);
});
});
Upvotes: 2
Views: 275
Reputation: 76774
We'd need to see your form
partial to be clear, but from first glance, you have an issue with your update
action:
def update
if @category.update(category_params)
redirect_to admin_categories_path, notice: "Category saved"
else
render :edit, error: "Something was wrong"
end
end
You had @article.update
- I don't know why, because you haven't even set @article
at all.
--
Update
It seems that your form is the cause of the issue:
= simple_form_for @category, url: admin_categories_path, method: 'post' do |f|
Here, you're sending each request to the create
action.
You need to use the following:
= simple_form_for [:admin, @category] do |f|
Upvotes: 2
Reputation: 265
As someone noticed, you are using @article variable that seems to be undeclared. This should cause an ApplicionError, so I think this maybe a simple bug, that your "edit" link actually leads to the "new" action. Does the name field actually contains a category name, or is it empty?
UPDATE: Actually, using @article will not cause an error. But check your links to be sure anyway.
Upvotes: 0