Issues creating a model instance - TypeMismatch error in Ruby on Rails -
i'm getting error , don't understand why. have form passes control input create model instance with.
here form:
=form_for :message, :url => {:action => 'create', :controller => 'messages'} |f| =f.text_area :body, :placeholder => 'enter message.', :size => '40x5' =f.hidden_field :subject, value: "re:#{@message.subject}" =f.hidden_field :parent, value: @message.id =f.hidden_field :receiver, value: @message.sender =f.submit 'reply'
here controller:
def create user = user.find_by_username(params[:message][:receiver])#remove receiver , give varibale -rg 4/29 if user ||= user.find_by_id(params[:message][:receiver]).username#valid entry -rg 4/29 params[:message].delete('receiver') params[:message][:status] = 'unread'#set status -rg 4/29 params[:message][:sender] = @current_user#set sender -rg 4/29 # raise params.inspect 13 => @message = message.create!(params[:message])#create message! -rg 4/29 if parent = params[:message][:parent]#checks if reply message -rg 4/29 message.find_by_id(parent).update_attributes(:child => @message)#updates parent message have message child -rg 4/29 end @message.received<<user#adds receiver database flash[:notice] = "message sent." redirect_to messages_path else flash[:warning] = "there no user username." redirect_to messages_path end end
and message model:
class message < activerecord::base attr_accessible :body, :subject, :status, :sender, :receiver,:parent, :child belongs_to :sent, :class_name => 'user' has_and_belongs_to_many :received, :class_name => 'user' belongs_to :parent, :class_name => 'message' has_one :child, :class_name => 'message' end
the error getting, including trace:
activerecord::associationtypemismatch in messagescontroller#create message(#41016000) expected, got string(#17696120) rails.root: /home/renee/classes/se/brainstormy application trace | framework trace | full trace app/controllers/messages_controller.rb:13:in `create'
here params being passed:
{"utf8"=>"✓", "authenticity_token"=>"ylytxntjludk15dm554vpjom7hlqcc2d+hgmkuljcee=", "message"=>{"body"=>"j", "subject"=>"re:j", "parent"=>"1", "status"=>"unread", "sender"=>2}, "commit"=>"reply"}
it's interesting because have second form uses same controller works perfectly. seems field have, :parent
culprit. want don't want pass message. want pas integer id. it's getting passed string. can me fix this?
this because passing in string (f.hidden_field :parent, value: @message.id
) parent association method requires message object. want set foreign_key field parent association message id.
if foreign_key parent_id, hidden field should this:
f.hidden_field :parent_id, value: @message.id
you'll have check if making :parent
accessible same making :parent_id
accessible otherwise won't set.
Comments
Post a Comment