我在应用程序的管理面板上编辑对象时遇到问题。 (这是一个政治相关的应用程序,因此提到自由党和保守党等)。
当我编辑类别时,它会创建一个新类别(具有相同的名称),而不是更新它。我无法弄清楚为什么。
我可以从日志中看到,当我编辑类别时,Rails似乎转到POST路由(POST /admin/categories(.:format) admin/categories#create)
而不是PUT或PATCH路由(PATCH /admin/categories/:id(.:format) admin/categories#update)
如果我使用的术语不正确,我会道歉,我不熟悉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(使用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'
我认为这些文件中的某个文件有问题,但是如果我还需要复制那些文件,请告诉我。谢谢!
_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);
});
});
答案 0 :(得分:2)
我们需要看到您的#selecting points in a square
condition=(xarr>xmin) & (xarr<xmax) & (yarr>ymin) & (yarr<ymax)
#depending on what you want, coordinates or value at coordinates
xsq=xarr[condition]
ysq=yarr[condition]
squaredata=data[condition]
#for ellipse:
#x0, y0, a and b can be preset if only this function.
in_ellipse=np.vectorize(\
lambda x,y,x0,y0,a,b: np.sqrt(((x-x0)/a)**2 + ((y-y0)/b)**2)<=1.0)
ellipsedata=data[in_ellipse(xarr,yarr,1.6,-1125,0.1,10)]
x_ellipse=xarr[in_ellipse(xarr,yarr,1.6,-1125,0.1,10)]
y_ellipse=yarr[in_ellipse(xarr,yarr,1.6,-1125,0.1,10)]
部分明确,但乍一看,您的form
行动存在问题:
update
你有def update
if @category.update(category_params)
redirect_to admin_categories_path, notice: "Category saved"
else
render :edit, error: "Something was wrong"
end
end
- 我不知道为什么,因为你根本没有设置@article.update
。
-
<强>更新强>
您的表单似乎是问题的原因:
@article
在这里,您要将每个请求发送到= simple_form_for @category, url: admin_categories_path, method: 'post' do |f|
操作。
您需要使用以下内容:
create
答案 1 :(得分:0)
正如有人注意到的那样,你正在使用似乎未声明的@article变量。这应该会导致ApplicionError,所以我认为这可能是一个简单的错误,你的&#34;编辑&#34;链接实际上导致&#34;新&#34;行动。名称字段实际上是否包含类别名称,还是为空?
更新:实际上,使用@article不会导致错误。但无论如何,请检查您的链接。