Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check variable is being passed in partial

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 60
    Comment on it

    While working in rails we make use of partials very often and there are case where we make use of same partial for different scenario. We try to achieve the same by passing default parameter for some unwanted variable which might case issue. Passing the default value for that variable in all the location you render the partial is tedious and error-prone.

    Instead of this we can catch whether a variable have been passed for a particular partial or not but we can't use defined? operator to do this. The ActionView documentation says it's an implementation restriction.

    So here is the way you can check whether the variable is available. Use local_assigns Hash.

    <% if local_assigns.has_key? :name %>
      

    <%= name %>

    <% end %>

    The local_assigns Hash contains all the locals you passed to the partial

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: