2014-12-04 58 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