每個用戶has_many:通知,並且每個通知has_many:comments,每個通知類。每條通知/評論has_one:複審通知,正在進行評論的通知。 我在嘗試提交評論時收到此錯誤消息:undefined method 'id' for "#<Notice:0x0000010eed6958>":String
。爲什麼Rails認爲我的Notice
是String
?Rails:undefined方法`id'
notices_controller.rb:
def create
@notice = user.notices.build(notice_params)
if @notice.save
if !params[:notice][:commentee].nil?
@notice.create_comment(params[:notice][:commentee])
end
end
end
private
def notice_params
params.require(:notice).permit(:content, :comentee)
end
notice.rb:
belongs_to :user
has_one :active_comment_relationship, class_name: "Commentrelationship", foreign_key: "commenter_id", dependent: :destroy
has_one :supernotice, through: :active_comment_relationship, source: :commentee
def create_comment(other_notice)
active_comment_relationship.create(commentee_id: other_notice.id)
end
要在通知提交評論中_notice.html.erb:
<%= form_tag({controller: "notices", action: "create"}, method: "post", id: "comment_form") do %>
<%= hidden_field_tag :callsign, @user.callsign %>
<%= hidden_field_tag "notice[commentee]", notice %>
<%= text_area_tag "notice[content]", '', id: "commentField" %>
<%= submit_tag "Reply" %>
<% end %>
服務器日誌:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "callsign"=>"bazzer", "notice"=>{"commentee"=>"#<Notice:0x00000110a49140>", "content"=>"Hello there."}, "commit"=>"Reply"}
您不能在參數'<%= hidden_field_tag>通知[commentee]「中註冊對象,注意%>','」commentee「=>」#<注意:0x00000110a49140>',它會變成字符串。 – roxxypoxxy