I have a form that uses two models - badge and badge winners. The user_id
and community_id fields are needed for badge_winners which is nested in
badges. I was trying to learn from Ryan Bates' Railscast (
http://railscasts.com/episodes/196-nested-model-form-part-1)

The error I'm getting is:
ActiveRecord::UnknownAttributeError in BadgesController#create
unknown attribute: user_id

app/controllers/badges_controller.rb:42:in `new'

LIne 42 corresponds to this line in :create
@badge = Badge.new(params[:badge])

*here's the controller:*

def new
@badge = Badge.new
badge_winner = @badge.badge_winners.build
respond_with(@badge)
end

def create
@badge = Badge.new(params[:badge])
if @badge.save
flash[:notice] = "Badge was successfully created."
redirect_to home_path
else
flash[:notice] = "There was a problem creating your badge."
redirect_to home_path
end
end


*Here's the form (both community_id and user_id are populating fine):*

<%= form_for(@badge) do |f| %>

<%= f.label :Description %>
<%= f.text_area :description %>

<%= f.fields_for :badge_winners do |builder| %>
<%= builder.hidden_field :user_id ,:value => user_id %>
<%= builder.hidden_field :community_id ,:value => community_id %>
<% end %>

<%= f.submit "Give Badge" %>
<% end %>

*The models (user_id and community_id are fields in the BadgeWinner table):*

class Badge < ActiveRecord::Base
belongs_to :community
has_many :badge_winners, :dependent=>:destroy
accepts_nested_attributes_for :badge_winners
end

class BadgeWinner < ActiveRecord::Base
belongs_to :user
belongs_to :badge
end

I'm hoping someone's able to help. I just know it's a newbie error
somewhere. Thanks in advance for any assistance!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V1kKNdUcaU4J.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 9, '12 at 6:49a
activeMar 9, '12 at 6:49a
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Yellowreign: 1 post

People

Translate

site design / logo © 2021 Grokbase