Hey all, this is a pretty simple (I think) question regarding forms
I have two models, one is message.rb
class Message < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id
end
and the second is user.rb
class User < ActiveRecord::Base
has_many :messages
end
Then I also have a form which will let me specify which User a Message
belongs to.
<p><label
for="message_content">Content</label><br/>
<%= text_field ''message'', ''content''
%></p>
<p>
<label for="message_user_id">User</label><br/>
<%= select("user", "id", User.find(:all).collect {|u|
[u.username, u.id]
}) %>
</p>
And here''s the create method
def create
@message = Message.new(params[:message])
if @message.save
@message.user = User.find(params[:user_id])
flash[:notice] = ''Message was successfully created.''
redirect_to :action => ''list''
else
render :action => ''new''
end
end
I can list the available users in the database, but I''m having a hard
time retrieving the value from the select box and using it to set
message.user_id
The erroneous code is probably in the create method, but I can''t figure
out what I''m doing wrong. I even tried
@message.user_id = 1 - which obviously didn''t work either.
Any takers on this pretty simple question?
Thanks!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---