从另一个控制器渲染操作时出错

时间:2013-06-09 08:08:09

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

在我的待办事项列表应用程序中,我在ListsController中有以下代码:

 def create
  @list= current_user.lists.build(params[:list])
  if @list.save
    flash[:notice]= "New List created"
    redirect_to controller: "pages",action: :home
  else
   render 'pages/home'
  end
end

页/ home.html.erb

 <% if signed_in? %>
    <%= render 'home_signed_in' %>
 <% else %>
   <%= render 'home_not_signed_in' %>
<% end %>

“home_signed_in.html.erb”和“home_not_signed_in.html.erb”出现在pages目录中。因此,当我尝试创建的列表不会被保存时,此代码将在ListsController中执行:

 else
   render 'pages/home'
  end

以下错误被抛出:

Missing partial lists/home_signed_in, application/home_signed_in with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/home/lnreddy/my_rails_projects/todo_app/app/views"

为什么不在页面/文件夹中搜索?我很困惑。

1 个答案:

答案 0 :(得分:1)

在控制器中使用render不会呈现该操作,而是呈现视图

您正在ListsController的上下文中呈现pages/home,因此当该视图查找部分'home_signed_in'时,它会查找当前控制器的文件夹,即列表控制器(以及列表文件夹) )。

如果要确保视图从同一文件夹呈现部分,请在文件夹名称前加上,例如。 '家/ home_signed_in'。或者,如果要在多个控制器之间共享partial,请将其移动到应用程序文件夹(所有视图都会回退到从此文件夹中读取,如错误消息列表所示)。

有关部分内容的更多信息,请参阅http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

相关问题