Каждый пользователь has_many: уведомления и каждое уведомление has_many: комментарии, каждый из класса Notice. Каждое уведомление/комментарий has_one: суперноти, уведомление, по которому делается комментарий. Я получаю это сообщение об ошибке при попытке отправки комментария: undefined method 'id' for "#<Notice:0x0000010eed6958>":String
. Почему Rails считает мой Notice
String
?Rails: undefined method `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"}
Вы не можете передать объект в params '<% = hidden_field_tag" уведомление [commentee] ", notice%>', '" commentee "=>" # <Примечание: 0x00000110a49140> "', он становится строкой. – roxxypoxxy