2014-12-04 38 views
0

我無法理解Create Method上的雙渲染錯誤。創建時的雙渲染錯誤

問題是Create Method必須重定向到一個非特定的對象,因爲Create Method的對象有一個多態類(Commentary - Commentable)。

我想創建一個評論,將它鏈接到一個對象(Appointment,Person,...),並重定向到該對象(redirect_to @objeto),而不是評論索引。我該怎麼辦?

代碼(comentarios_controller.rb)主要是:

def create 

    #Verifica o objeto 
    @objeto = nil 
    tipo = params[:comentable_type] 
    case tipo 
     when "Comercial::Oportunidade" 
      @objeto = Comercial::Oportunidade.find_by_id(params[:comentable_id]) 
     when "Comercial::Compromisso" 
      @objeto = Comercial::Compromisso.find_by_id(params[:comentable_id]) 
    end 

    @comentario=Comercial::Comentario.new(params[:comercial_comentario]) 
    @comentario.organizacoes<<current_empresa 
    @comentario.usuario = current_usuario 

    create! do |success, failure| 
    if success 
    @objeto.comentarios << @comentario 
    flash[:success] = I18n.t 'activerecord.successful.messages.created.m', :model => @comentario.class.model_name.human 
    redirect_to @objeto 
    else 
    flash.discard 
    end 
end 
end 

使用Rails 3.2.2

+0

哪裏是創建代碼! ? – 2014-12-04 17:18:32

+0

它來自InheritedResources(https://github.com/josevalim/inherited_resources)。 – Laerte 2014-12-04 18:22:51

回答

1

我不是很熟悉繼承的資源(我看到筆者不再建議其使用),但它似乎確實在這裏沒有正確使用它。給出的例子是最相似的您的使用情況是這樣的:

class ProjectsController < InheritedResources::Base 
    def update 
    update! do |success, failure| 
     failure.html { redirect_to project_url(@project) } 
    end 
    end 
end 

注意的方式,它使用failure參數 - 它不是一個布爾值,想必它希望採取像這裏顯示的一個塊:{ redirect_to ... }。所以我認爲你應該嘗試重寫這部分看起來更像:

create! do |success, failure| 
    success.html { 
    @objeto.comentarios << @comentario 
    flash[:success] = I18n.t 'activerecord.successful.messages.created.m', :model => @comentario.class.model_name.human 
    redirect_to @objeto 
    } 
    failure.html { flash.discard } 
end 
+0

它工作完美! – Laerte 2014-12-04 19:16:10

+1

酷!很高興聽到它的工作。 – 2014-12-04 19:17:44