无法使用'render'在我的控制器中调用另一个方法

时间:2011-08-21 09:58:25

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试创建一个多按钮表单。如果我按“创建检查”它确实创建副本是它应该做的。我创建了路线。

我的控制器:

  def create_multiple
    @webhost = Webhost.find(params[:webhost_ids])
    if params[:delete_button]
     render 'admin/webhosts/delete_multiple'
    else
    @webhost.each do |webhost|
    Webhost.create(webhost.attributes)
    end
    end
    respond_to do |format|
      format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') }
      format.xml  { head :ok }
    end
end

def delete_multiple
    @webhost = Webhost.find(params[:webhost_ids])
    @webhost.each do |webhost|
    webhost.destroy
    end
    respond_to do |format|
      format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') }
      format.xml  { head :ok }
    end
end  

在我看来,我有:

<%= submit_tag "Create Checked" %>  
<%= submit_tag "Delete Checked", :name => 'delete_button' %>  

当我按下删除选中按钮时出现此错误: 模板缺失

Missing template admin/webhosts/delete_multiple with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "C:/Rails/webhostapp/app/views", "C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.1.5/app/views", "C:/Ruby192/lib/ruby/gems/1.9.1/gems/kaminari-0.10.4/app/views", "C:/Rails/webhostapp", "C:/"

网址为:http://localhost:3000/admin/webhosts/create_multiple

1 个答案:

答案 0 :(得分:2)

如果我看得正确,您想在点击按钮时拨打delete_multiple

如果delete_multiple是您的控制器的一种方法,则不会通过render 来调用它。 render会尝试查找名为admin/webhosts/delete_multiple.html.erb或类似的视图并显示该视图。有关详情,请参阅Layouts and Rendering in Rails

您可能想要做的就是像往常一样调用此方法:

def create_multiple
    @webhost = Webhost.find(params[:webhost_ids])
    if params[:delete_button]
     delete_multiple(params) and return
    else
    # ...
end

请注意,您可能需要将参数传递给delete_multiple方法。您必须对其进行调整以将参数作为参数:

def delete_multiple(params)
    @webhost = Webhost.find(params[:webhost_ids])
    @webhost.each do |webhost|
    webhost.destroy
    end
    respond_to do |format|
      format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') }
      format.xml  { head :ok }
    end
end