gpt4 book ai didi

ruby-on-rails - 为什么这种形式认为它应该路由到索引?

转载 作者:行者123 更新时间:2023-12-04 06:11:49 24 4
gpt4 key购买 nike

我有一个 simple_form_for new_comment 导致浏览器中出现 undefined methodcomments_path'` 错误,当我只是尝试查看表单(而不是提交)时

_form.html.slim

= simple_form_for new_comment, :remote => true do |f|

这里是partial,所以传递的局部变量来自hacks scaffold的show page

show.html.slim - 技巧

= render partial: "comments/form", locals: { new_comment: @new_comment } if user_signed_in?

我在 hacks Controller 中定义了@new_comment

hacks_controller.rb

  def show
@hack = Hack.find(params[:id])
@comments = @hack.comment_threads.order('created_at DESC')
@new_comment = Comment.build_from(@hack, current_user.id, "") if user_signed_in?
#build_from is a method provided by the acts_as_commentable_with_threading gem
end

为什么 new_comment 想要路由到 comments_path?我什至没有提交表格。

routes.rb

  root 'hacks#index'

concern :commentable do
resources :comments, only: [:create, :update, :destroy]
end

resources :hacks, concerns: [:commentable]
resources :users

devise_for :users, :skip => [:sessions, :registration]
devise_for :user, :path => '', :path_names => { :sign_in => "login",
:sign_out => "logout",
:sign_up => "register",
:account_update => "account-settings" }

最佳答案

由于您的评论嵌套在 hack 中,因此您需要评论和 hack。所以,试试这个

show.html.slim

= render partial: "comments/form", locals: { new_comment: @new_comment, hack: @hack } if user_signed_in?

_form.html.slim

= simple_form_for [hack, new_comment], :remote => true do |f|

关于ruby-on-rails - 为什么这种形式认为它应该路由到索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821007/

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