views:

28

answers:

1

I have application.html.erb in app/views/layouts directory.

application.html.erb

if @condition
  <%= yield %>
end

index.html.erb in app/views/item directory

<% @value.id %>

item_controller.rb

def index
  @value = nil
  if @condition
    @value = my_value
  end

  respond_to do |format|
    format.html
  end
end

if @condition is false /localhost:3000/item render error that @value is nil. Why? I don't have layout file for item and if @condition is true, it works okay.

Is index.html.erb checked even though application.html.erb does not yield?

+2  A: 

I do not entirely understand what you are doing here. But seems you need to handle for the case when @value is nil.

You are using this line <% @value.id %> and trying to get 'id' for @value which will be nil if the @condition is false.

I guess you have missed the tag

<% if @condition %>
  <%= yield :layout %>
<% end %>
Bragboy
I don't need to show all of my source code :). anyway, What I wonder is why index.html.erb is rendered when @condition is equal to false. help anybody please ~
Hongseok Yoon
@xopht : Look at my edited answer, however if you need a comprehensive result/understanding, why dont u post ur full code, the steps you are trying to do, so that I can debug in Netbeans and let you know.
Bragboy
I think..., because application.html.erb is a part of views, <%= yield %> does not evaluate the condition and just render index.html.erb. So, I've just wrapped whole code in index.html.erb with <% if @condition %> ... <% end %>. This work okay. any other solutions?
Hongseok Yoon