Topic: issue passing objects to partials

my error partial
<% if @object.errors.any? %>
    <div id="error_explanation">
            <%= pluralize(@object.errors.count, "error") %>
            prohibited this object from being saved:
        <p>There were problems with the following fields:</p>
            <% @object.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
<% end %>

the main form
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
    <%= render 'shared/error_messages', :object    => f.object %>
    <%= render 'layouts/fields', :f => f %>
    <div class ="actions">
        <%= f.submit "sign up" %> <br/>
<% end %>

i get "undefined method `errors' for nil:NilClass"

any help is appreciated ...
thanks J !!

Last edited by bicepjai (2011-06-05 08:54:09)

Re: issue passing objects to partials

Hi bicepjai,
First of all, class level instance variables are automatically passed to the model, view, and it's partials. (It's the variables with the @ sign in front of them.) Variables without the @ sign, are not automatically passed.

Second, an easier way to pass a variable into a partial is like so...

<%= render :partial => 'shared/error_messages', :locals => {:object => f.object} %>

And then you can just say <% if object.errors.any? %> in the 'error_messages' partial.

hm... I hope that helped. smile
Let me know if you need more help, I'd like to help, I was where you are once!


Last edited by CodaBrink (2011-06-14 23:06:05)