Rails 3 - 渲染部分

时间:2012-09-30 16:54:28

标签: ruby-on-rails-3

我在Rails中部分渲染时遇到了一些问题。

这是在我的routes.rb:

namespace :blog do
  resources :posts, only: [:index, :show] do 
    resources :comments, only: [:new, :create]
  end
end

这是我的Blog :: PostsController:

def show
  @post = Post.find(params[:id])
  @comments = @post.comments
end

这是在/views/blog/posts/show.html.erb

<%= render @comments %>

_comment.html.erb partial位于/ views / blog / comments /

错误消息是:

Missing partial blog/comments/comment with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/mar1221/ruby/my_site/app/views"

1 个答案:

答案 0 :(得分:1)

通常你会传递部分名称(没有下划线),如:

<%= render 'comment' %>

将尝试从视图路径中呈现部分_comment.html.erb

partial可以与父视图位于同一目录中,位于共享目录中,也可以位于视图路径中包含的任何其他目录中。

退房:

http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials有更详细的说明,以及渲染部分内容时可以使用的其他选项。

相关问题