Rails更新方法不工作:ActionView::MissingTemplate (Missing template i



以下是我在items_controller中的更新方法:

def update
    @item = Item.find(params[:id])
    if @item.update_attributes(item_params)
      flash[:success] = "Item updated"
      redirect_to edit_item_path(@item)
    else
      render edit_item_path(@item)
    end
  end

日志中@item似乎加载成功,之后出现Completed 500 Internal Server Error in 73ms:

Item Load (6.7ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = ? ORDER BY "items"."created_at" DESC LIMIT 1  [["id", 30]]
Completed 500 Internal Server Error in 54ms
ActionView::MissingTemplate (Missing template items/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
 * "/Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
 * " # app route # "
 * "/Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/app/views"
 ):
 actionview (4.2.0) lib/action_view/path_set.rb:46:in `find'
 actionview (4.2.0) lib/action_view/lookup_context.rb:121:in `find'
 actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:18:in    `find_template'
 etc.

似乎@item.update_attributes(item_params)可能没有做任何事情。如何解决这个问题?

应该如下:

def update
    @item = Item.find(params[:id])
    if @item.update_attributes(item_params)
      flash[:success] = "Item updated"
      redirect_to edit_item_path(@item)
    else
      render :edit  # this line should be changed
    end
  end

相关内容

  • 没有找到相关文章

最新更新