Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails创建新对象而不是更新它_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails创建新对象而不是更新它

Ruby on rails Rails创建新对象而不是更新它,ruby-on-rails,Ruby On Rails,我在应用程序的管理面板中编辑对象时遇到问题。(这是一款与政治相关的应用程序,因此提及自由派和保守派等)。 当我编辑一个类别时,它会创建一个新类别(具有相同的名称),而不是更新它。我不明白为什么。 我可以从日志中看到,当我编辑类别时,Rails似乎转到POST路由(POST/admin/categories(:format)admin/categories#create),而不是PUT或PATCH路由(PATCH/admin/categories/:id(:format)admin/categor

我在应用程序的管理面板中编辑对象时遇到问题。(这是一款与政治相关的应用程序,因此提及自由派和保守派等)。 当我编辑一个类别时,它会创建一个新类别(具有相同的名称),而不是更新它。我不明白为什么。 我可以从日志中看到,当我编辑类别时,Rails似乎转到POST路由
(POST/admin/categories(:format)admin/categories#create)
,而不是PUT或PATCH路由
(PATCH/admin/categories/:id(:format)admin/categories#update)

抱歉,如果我使用了不正确的术语,我不太熟悉Rails

类别\u 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
我想其中一个文件可能有问题,但如果我需要复制其他文件,请告诉我。谢谢

_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);
    });
  });

我们需要查看您的
表单
部分才能清楚,但乍一看,您的
更新
操作有问题:

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
操作

您需要使用以下各项:

= simple_form_for [:admin, @category] do |f| 

有人注意到,您使用的@article变量似乎未声明。这应该会导致一个应用程序错误,所以我认为这可能是一个简单的错误,您的“编辑”链接实际上会导致“新建”操作。名称字段是否实际包含类别名称,还是为空


更新:实际上,使用@article不会导致错误。但请检查您的链接以确定是否有任何问题。

在更新操作中,您有
@article.save
,而您真正想要的是,是
@category.save
你能在你的问题中显示部分表单吗?@AndreyDeineko我试过了,但它仍然在创建一个新的类别。@SteveTurczyn在部分表单中添加了内容。我试过了,但仍然是相同的问题。当我保存它时,一个同名的新类别是CreatedHanks buddy,让我们检查一下这个,这就是Rich!做得好。谢谢你。为没有提前提交表格向其他人道歉。我不认为这是本例中问题的一部分。@article在articles\u控制器中声明。实际上,我不确定这在这个问题上是好是坏,这与此无关,但它会产生另一个问题。您不能在另一个控制器中声明它。如果要从“类别编辑”视图访问文章,需要在“类别控制器”中声明。谢谢。实际上,我不需要从类别编辑视图访问文章。
= simple_form_for [:admin, @category] do |f|