public
anonymous / \app\models\user.rb
Created

Server error log when running accepts_nested_attributes_for

  • Download Gist
\app\models\user.rb
Ruby
1 2 3 4 5 6 7 8
 
class User < ActiveRecord::Base
has_many :events, :dependent => :destroy
accepts_nested_attributes_for :events, :reject_if => lambda { |attributes| attributes['event_name'].blank? }, :allow_destroy => true
 
attr_accessible :name
end
app/controllers/users_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class UsersController < ApplicationController
respond_to :html, :xml, :js
def index
@users = User.all
end
 
def new
debugger
@user = User.new
1.times do
event = @user.events.build
#event = @user.build_event
end
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "New user created"
redirect_to user_url(@user)
else
flash[:notice] = " User not created"
redirect_to new_user_url
end
end
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flsh[:notice] ="successfully updated."
redirect_to @user
else
render :action => 'new'
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
flsh[:notice] ="successfully deleted user."
redirect_to @user
end
app/views/users/index.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 
<% title "Events" %>
<table >
<tr>
<th><%= sortable "Starts_at" %></th>
<th><%= sortable "Finishes" %></th>
<th><%= sortable "Event_name" %></th>
<th><%= sortable "Tracks" %></th>
</tr>
<td>
<% @user.events.each do |event| %>
<tr>
<td><%= event.starts_at %></td>
<td><%= event.finishes %></td>
<td><%= event.event_name %></td>
<td><%= event.tracks %></td>
</tr>
<% end %>
 
</table>
<%= link_to "Add new event", new_user_path %>
 
<%= button_to "Delete", @user,:confirmation => "Are you sure?", :method => :delete %>
form partial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<label>Name of event:
<p> <%= builder.text_area :event_name, :rows => 1 %> </p>
</label>
<label>Tracks
<p><%= builder.text_area :tracks, :rows => 1 %><p>
</label>
<label>Event starts:
<p><%= builder.text_area :starts_at, :rows => 1 %> <p>
</label>
<label>Event Finishes:
<p><%= builder.text_area :finish, :rows => 1 %><p>
</label>
</p>
<% end %>
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
ActionView::Template::Error (undefined method `events' for nil:NilClass):
9: </tr>
 
10: <td>
 
11:
 
12: <% @user.events.each do |event| %>
 
13: <tr>
 
14: <td><%= event.starts_at %></td>
 
15: <td><%= event.finishes %></td>
 
app/views/users/index.html.erb:12:in `_app_views_users_index_html_erb___603337143_36309060_0'
 
Rendered C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" ORDER BY name asc
Rendered C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (15.6ms)
Rendered C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (62.5ms)
schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ActiveRecord::Schema.define(:version => 20101201180355) do
 
create_table "events", :force => true do |t|
t.string "event_name"
t.string "tracks"
t.datetime "starts_at"
t.datetime "finish"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
 
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.