0

Migrating app from php. and have this in the view:

<%=render :partial => "jt-test", :locals => {:info => "here is my info", :hide_location=>true} %>
<br /><br />
<%=render :partial => "jt-test", :locals => {:info => "here is my info"} %>

in _jt-test.html.erb:

My info:<br /> 
<%=info %>

<% if local_assigns.has_key? :hide_location %>
    you want to hide location!
<% end %>

Is the local_assigns the proper / best way to do this? Can I have an unlimited number of local_assigns? Is there a local_assigns for the main view called from the controller?

thx

timpone
  • 19,235
  • 36
  • 121
  • 211
  • possible duplicate of [optional local variables in rails partial templates: how do I get out of the (defined? foo) mess?](http://stackoverflow.com/questions/2060561/optional-local-variables-in-rails-partial-templates-how-do-i-get-out-of-the-de) – Ciro Santilli OurBigBook.com Sep 30 '14 at 16:15

3 Answers3

1

In the main view you'd just use normal action class variables (@whatever_variable_name), and they're assigned in the controller:

class FoosController
  def index
    @foos = Foo.all
  end
end

# template
<% @foos.each |foo| %>
    <%= foo.name %>

You may have unlimited locals in a partial, but if there are a "lot", you might be doing it wrong. consider using an encapsulating object, breaking up the template more, etc.

Rails exposes local variables to partials by their name (info and hide_location in your case). You don't need to look it up using has_key?. See the passing local variables docs in the layout and rendering guide.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • You should look up those variables with `has_key?` if they are optional. That is even recommended. Also, it's best practice to reuse partials with local variables and not rely on unknown instance variables. – Draiken Oct 21 '11 at 16:50
  • @Draiken Sure, if they're optional. The comment about instance variables was targeted at OP's question about "local_assigns for the main view called by the controller", and is correct. – Dave Newton Oct 21 '11 at 16:53
  • appreciate the work but I'm not really seeing any discussion of optional arguments to partials in the guide. may be I just missed it – timpone Oct 21 '11 at 17:34
  • @timpone I don't think that's discussed in the guide; I'd use simple map notation instead of `has_key?` -- I just missed that in your question. Here's a [comment on apidock](http://apidock.com/rails/ActionController/Base/render#628-Optional-local-assigns) that mentions it, and [here's an SO post](http://stackoverflow.com/questions/2060561/optional-local-variables-in-rails-partial-templates-how-do-i-get-out-of-the-de) that has some other info. The [ActionView docs](http://api.rubyonrails.org/classes/ActionView/Base.html) also has bits of info. – Dave Newton Oct 21 '11 at 18:49
1

Rails 3 improves the way you can render partials.

<%= render "jt-test", :info => "here is my info", :hide_location => true %>
<br /><br />
<%= render "jt-test", :info => "here is my info", :hide_location => false %>

in _jt-test.html.erb:

My info:<br /> 
<%= info %>

<% unless hide_location %>
  you want to hide location!
<% end %>

Is the local_assigns the proper / best way to do this?

The above is the preferred way of rendering partials with Rails 3

Can I have an unlimited number of local_assigns?

You're limited by memory.

Is there a local_assigns for the main view called from the controller?

Not sure but why would you want to do that. Keep view logic out of the controllers.

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
  • this looks like better syntax than the :locals – timpone Oct 21 '11 at 16:53
  • Hmm.... I'm getting an `undefined local variable or method 'hide_location' ` error. I'm sure this is probably an error on my part – timpone Oct 21 '11 at 17:20
  • No, it's an error on my part. You can just keep the variables consistent and assign nil to them so you dont get the error like you did. – Chris Ledet Oct 21 '11 at 17:24
0

Like Dave said, if you have a lot of optional variables, you might be better off encapsulating an object and passing it to the partial, but in the case of checking for optional local assigns, the has_key? method is the best way to go.

Draiken
  • 3,805
  • 2
  • 30
  • 48