Rails:更新模型属性。错误缺少模板

时间:2017-01-28 02:24:05

标签: ruby-on-rails

我正在尝试创建一个页面来编辑/更新记录,但是我收到了这个错误:

Missing template tickets/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ubuntu/workspace/app/views" 

我的门票控制器:

    class TicketsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy, :index]
  def index
    @tickets = Ticket.all
  end

  def show
        @ticket = Ticket.find(params[:id])

  end

  def edit 
    @ticket = Ticket.find(params[:id])
  end

  def update
    @ticket = Ticket.find(params[:id])
    if @ticket.update_attributes(ticket_params)
      flash[:success] = "Ticket atualizado!"
      redirect_to @ticket
    else
      render 'edit'
    end
  end

  def new
    @ticket = Ticket.new
    @user = current_user
  end

  def create
    @ticket = Ticket.new(ticket_params)
    @ticket.user = current_user

    if @ticket.save
      redirect_to @ticket
    else
      render :new
    end
  end

  def destroy
  end

  def update
  end

  private

  def ticket_params
    params.require(:ticket).permit(:subject, :body, :status)
  end

end

我的观点edit.html.erb

<h1>Editar Ticket</h1>

<h2><%= @ticket.subject %></h2>

<div class ="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@ticket) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
      <%= f.label :status %>
      <%= f.select :status, ['Aberto', 'Resolvido',  'Fechado', 'Processando']%>

      <p><%= @ticket.body %></p>

      <%= f.submit "Salvar", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

服务器日志:

    Processing by TicketsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ljRnDwjCpKhL+Jl4PkQbLiyEESHnVMgQjpt2EJ6QoEhMza03feBRzz3xOAFsjnjWz7+ASAuGn1qKx+gHtUIm7w==", "ticket"=>{"status"=>"Fechado"}, "commit"=>"Salvar", "id"=>"16"}
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)

ActionView::MissingTemplate (Missing template tickets/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

我是根据同一应用程序中的另一个模型编写的,它正在更新没有错误的属性。 已经尝试过更改为@ticket.update_attributes(params[:ticket])以及其他有关stackoverflow的建议,但没有幸运。

我在这里缺少什么?

抱歉我的英文不好。

1 个答案:

答案 0 :(得分:2)

Rails中控制器操作的行为是render by default。发生这种情况时,Rails将查找与操作同名的模板。

Ruby允许您使用redefine methods,当您打算覆盖现有方法的行为时,这可能很有用。

在这种情况下,第二个update方法会覆盖您要执行的第一个update方法。第二个update方法什么都没做,所以默认情况下Rails尝试渲染名为tickets/update的模板。这导致Missing template tickets/update错误。

要解决此问题,您需要删除不需要的第二种方法。

相关问题