Hongseok Yoon
Hongseok Yoon

Reputation: 3318

Rails layout related problem

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?

Upvotes: 0

Views: 126

Answers (1)

bragboy
bragboy

Reputation: 35542

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 %>

Upvotes: 2

Related Questions