12

Here is how I pass the values/variable to the partial:

<%= render "partials/banner", :locals => {:text_1 => t(:"main.home.banner_text_1"),
                                          :text_2 => t(:"main.home.banner_text_2") } %>

then in the partial:

 <%= text_1 %> <%= text_2 %>

but getting "undefined local variable or method text_1"

Where should I set the variable so it could be accesible from all views and layouts in my app?

Thanks!

Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
bogumbiker
  • 437
  • 2
  • 5
  • 19
  • I'd be tempted to just put <%= t("main.home.banner_text_1") %> right in the partial -- can you get away with that? Or are they actually variable? – sarnold Jul 11 '10 at 10:09

2 Answers2

28

If you have something that has to be displayed across all your views you can also create a application_helper method, Example: banner('Text', 'Content')

Try this:

Main page:

<%= render :partial => 'layouts/test',
           :locals => {:text_1 => t(:'text_1'), :text_2 => t(:'text_2')}
%>

Partial:

<%= text_1 %> <%= text_2 %>
Warren Noronha
  • 599
  • 5
  • 4
  • 4
    thanks adding ":partial =>" finally solved the problem. Wondering why it does not work without? Anyway yes I need to have this displayed across all my views - can you provide more details how banner('text', 'content') helper method should look like? cheers – bogumbiker Jul 11 '10 at 13:00
12

I believe that Rails 3 has changed how you pass partial variables, to something like this:

<%= render :partial => 'layouts/test',
       :text_1 => t(:'text_1'), :text_2 => t(:'text_2') %>

Rails will parse that and since :text_1 is not a known key (like :collection or :as), it passes it to the partial itself.

You can access it via text_1 or text_2

Josh M.
  • 26,437
  • 24
  • 119
  • 200
Andrew K
  • 1,339
  • 15
  • 26