Partials and Models

Hi, I have a partial that needs to output the database data,

In partial:

<% for user in @users %>

NoMethodError in Users#create_user

Showing users/_user_list.html.erb where line #1 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

So does that mean the variable @users from the class User is unavailable
in my partial? If so, how do I bring it into context?

Thanks!

In your *controller* action, instanciate the @users instance variable,
eg:

@users = User.find(:all)

Alternatively, pass this through when you include the partial, eg:

<%= render :partial => 'some_partial', :locals => {:all_users =>
@users} -%>

..and use
<% for user in all_users %>
in your partial

Mmmm.. I still seem to be getting the same error message.

This is my controller create_user
  def create_user

    @user = User.new(params[:user])
      if @user.save
        render :update do |rjs|
          rjs.replace_html :userlist, :partial => 'user_list'
          flash[:notice] = "User was added successfully"
        end
      else
        redirect_to_index("<font color='red'>Error</font>")
      end
  end

view:
<div id="userlist">
</div>

partial:
<% for user in @users %>

        <tr class="<%= cycle('odd-row', 'even-row') %>">
          <td class="cell"><%= truncate(user.email, 15) %></td>
          <td class="cell"><%= truncate(user.password, 10) %></td>
            <td class="actions">
                <%= link_to 'View', user %></td>
            <td class="actions">
                <%= link_to 'Edit', edit_user_path(user) %> </td>
            <td class="actions">
                <%= link_to 'Remove', user, :confirm => 'Are you sure?',
:method => :delete %></td>
        </tr>

            <div id="<%= user.email %>">
              <tr class="row-details" style="display: none;">
                <td>sdf</td>
              </tr>
            </div>

<% end %>

Anything stand out as an error?

Thanks!

Hi --

Mmmm.. I still seem to be getting the same error message.

This is my controller create_user
def create_user

That's a very non-standard name. "create" is the usual name for the
method that does this.

   @user = User.new(params[:user])
     if @user.save
       render :update do |rjs|
         rjs.replace_html :userlist, :partial => 'user_list'
         flash[:notice] = "User was added successfully"
       end
     else
       redirect_to_index("<font color='red'>Error</font>")
     end
end

You still haven't initialized @users. (Did you see Phil's response?)
You're using an uninitialized variable in the view. It can't read your
mind :slight_smile: If you want it to contain an array of User objects, you have
to assign it the array.

David

I'm getting this strange message:

[snip]

That's the javascript generated by your rjs. Your action always
renders javascript and so if it requested by a normal request it will
just display the javascript

Something wrong with my form??

Yes, you're nesting a form inside a form. Don't do that.

Fred