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

On Jul 14, 10:40 am, Justin To [email protected]

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:

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 %>

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

<% end %>

Anything stand out as an error?

Thanks!

On 14 Jul 2008, at 02:15, Justin To wrote:

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

Hi –

On Mon, 14 Jul 2008, Justin To wrote:

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(“Error”)
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


Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

I’m getting this strange message:

try {
Element.update(“userlist”, "\n\n<div id=“add_user_form”>\n\n\n

\n testing...\n still t...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n [email protected]\n password\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n [email protected]\n yeeesss!\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n [email protected]\n password!\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n another_test...\n testing...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n jason_grauel...\n jasons_...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n added\n adddddd...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n sdfdsfsdfsdf\n sdafsad...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n another_user...\n let's_a...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n [email protected]\n qwertypoiu\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n gufufjgkjhkhl\n hgfjghk...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n without_java...\n wooootw...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n with_javascript\n YAY!!! ...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n addding\n refresh?\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n add_user\n yay no ...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n ok_testing_\n no_refr...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n jhhhoijhoiho\n lkjlsda...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n sdfsadfsadf\n sadfdfsdf\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n new user\n to be d...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n 123123123\n 123123123\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n add_new_user\n 12345678\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n 12312l3jh231\n hesfjddsf\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n\n \n sdhfdshfkjdsh\n jhj2435...\n \n View \n \n Edit \n \n \n\n Remove \n \n \n\n
\n \n sdf\n \n
\n\n\n"); } catch (e) { alert('

Something wrong with my form??

<% form_for :user, @user, :url => { :action => “create” } do |f| %>

<tr>
  <td class="text"><font class="actions-add">Email:</font></td>
  <td><%= f.text_field :email %></td>
</tr>

<tr>
  <td class="text"><font class="actions-add">Password:</font></td>
  <td><%= f.text_field :password %></td>
</tr>

  <tr>
    <td></td>
    <td align="right">
      <% form_remote_tag :url => { :action => :create } do %>
        <%= submit_tag "Add user" %>
      <% end %>
    </td>
  </tr>

</table>
<% end %>

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs