gpt4 book ai didi

ruby-on-rails - Form_For 未定义的方法路径

转载 作者:行者123 更新时间:2023-12-04 05:19:28 24 4
gpt4 key购买 nike

我正在试验嵌套资源:

我的路线:

  resources :conversations do
resources :replies do
resources :comments
end
end

我能够获得回复表单以处理对话,但现在我增加了获取评论以处理回复的额外复杂性。

整个表格都在对话显示路径下。
<%= form_for([@conversation, @reply]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>

上面的回复表单工作正常并且没有错误,下面的评论表单出现错误:

未定义的方法`reply_comments_path'
<%= form_for([@reply, @comment]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>

这是我用于展示的对话 Controller ,这就是我认为问题所在:
  def show
@conversation = Conversation.find(params[:id])
@replies = @conversation.replies
@reply = current_user.replies.build
#If I change the above line to @conversations.replies.build
#it breaks the ability to show replies above the form.

@comments = @reply.comments
@comment = @reply.comments.build
end

但是,其他人建议这样做:
<%= form_for([@conversation, @reply, @comment]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>

但它只以路由错误结束:
No route matches {:controller=>"comments", :format=>nil, :conversation_id=>#<Conversation id: 3, content: "Goes here.", user_id: 1, created_at: "2012-12-10 21:20:01", updated_at: "2012-12-10 21:20:01", subject: "Another conversation">, :reply_id=>#<Reply id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, conversation_id: nil>}

当我尝试制作新表单时,我总是遇到这个未定义的方法路径错误,而且我总是设法忘记我做错了什么。答案似乎永远不是路线。

编辑:

在 Controller 的创建部分下,我有:
@replies = @conversation.replies
@reply = current_user.replies.build
#If I change the above line to @conversations.replies.build
#it breaks the ability to show replies above the form.

我不知道为什么 @reply = @conversation.reply.build 破坏了显示现有回复的能力。我收到一条错误消息,说它无法将 nil 转换为数字,并且无法看到 reply.created_at 或 reply.content。无论是什么原因,这都可能是我遇到此问题的原因的线索。但是,在回复 Controller 中,我正在使用
@reply = conversation.replies.build(content: params[:reply][:content], user_id: current_user.id)

编辑:

补充一点,Stackoverflow 做的事情与我在这里尝试实现的非常相似,除了您可以对问题和答案发表评论。

最佳答案

看看你的错误的结尾:

... :reply_id=>#<Reply id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, conversation_id: nil>}

您无法为 @comment 创建表单如果 @reply没有保存。你需要坚持 @reply在创建 @comment 之前到它。

如果您还没有对 Reply 模型进行验证,请尝试对 show action 进行这个简单的测试:
# @reply = current_user.replies.build
@reply = current_user.replies.create

请参阅评论以获取答案。

关于ruby-on-rails - Form_For 未定义的方法路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13824682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com