Rails更新方法不起作用:ActionView :: MissingTemplate(缺少模板项/更新...)

时间:2015-07-05 18:45:12

标签: ruby-on-rails update-attributes

以下是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)可能没有做任何事情。有任何线索如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这应该是:

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