Why is a shared partial view in a nested layout not working in a Rails functional test?
I have the following code in app/views/foo/index.rhtml
:
<% render :layout => 'bar_group', :locals => {:id => 'gold_bars'} do %>
<%= render :partial => "bar_score_box", :locals => {:scores => scores} %>
<%= render :partial => "common/translate_bar_link", :locals => {:bar => bar} %>
<% end %>
_bar_group.rhtml
and _bar_score_box.rhtml
are both also in app/views/foo/
, but _translate_bar_link.rhtml
is in app/views/common/
.
This view works just fine when running my Rails app in development mode and hitting it with a web browser. But when I run a functional test that hits the view, I get an error like this:
Exception: Missing template _translate_bar_link.erb in view path
app/views/foo/_bar_group.rhtml:2
app/views/foo/_bar_group.rhtml
looks something like this:
<div id="<%= id %>" class="tile">
<%= yield %>
</div>
So the error is being reported on the yield
line.
Again, this works fine through the Rails server, but not when running a functional test. Does anyone have any idea what could be going on here?
Update: It turns out that I had a second render call for the same partial later in my view, which I forgot to change to common/translate_bar_link
when I moved the partial from 开发者_JS百科app/views/foo
to app/views/common
:
<%= render :partial => "translate_bar_link", :locals => {:bar => bar} %>
Operator error. Sorry for wasting your time. Flagging this question for deletion.
Try tinkering around with the path used in your render(:partial) statements.
Example (notice the slash in front of 'common/translate_bar_link'):
<% render :layout => 'bar_group', :locals => {:id => 'gold_bars'} do %>
<%= render :partial => "bar_score_box", :locals => {:scores => scores} %>
<%= render :partial => "/common/translate_bar_link", :locals => {:bar => bar} %>
<% end %>
Also, sometimes it helps to put the file extension, too, like this (notice the .rhtml):
<% render :layout => 'bar_group', :locals => {:id => 'gold_bars'} do %>
<%= render :partial => "bar_score_box", :locals => {:scores => scores} %>
<%= render :partial => "/common/translate_bar_link.rhtml", :locals => {:bar => bar} %>
<% end %>
I am not sure what version of Rails you are using, but maybe you can rename common/translate_bar_link.rhtml
to common/translate_bar_link.html.erb
精彩评论