Ruby on rails 如何设置Rails表单以提交到create方法?
我使用的是Rails 5,但对于如何设置表单以便表单提交给我的create controller方法感到困惑。这是我设定的路线Ruby on rails 如何设置Rails表单以提交到create方法?,ruby-on-rails,routes,ruby-on-rails-5,form-for,Ruby On Rails,Routes,Ruby On Rails 5,Form For,我使用的是Rails 5,但对于如何设置表单以便表单提交给我的create controller方法感到困惑。这是我设定的路线 resources :comments 这是我设置的表格 <%= form_for @comments, :html => {:class => "commentsForm"} do |f| %> <div class="field"> <%= f.label :description %><br&
resources :comments
这是我设置的表格
<%= form_for @comments, :html => {:class => "commentsForm"} do |f| %>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= recaptcha_tags %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
但是上面提到的都是错误的
undefined method `comments_index_path' for #<#<Class:0x00007fdaaf2b6240>:0x00007fdaaf2ae518>
我不确定我还应该做些什么来让我的表格生效。在我的控制器中,我有一个create和一个new方法
编辑:这是控制器
class CommentsController < ActionController::Base
before_action :require_current_user, except: [:new]
def new
@comments = Comments.new
end
def create
@comments = Comments.new(params[:comments].permit(:description))
if verify_recaptcha(model: @comments)
render "Finished"
end
end
end
我认为,您的应用程序的新操作应该是这样的-
# app/controllers/comments_controller.rb
class CommentsController < ApplicationController
# Rest of code here
def new
@comment = Comment.new # Not the @comments
end
# Rest of code here
end
你的表格应该是这样的
# app/views/comments/_form.html.erb
<%= form_for @comment, :html => {:class => "commentsForm"} do |f| %>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= recaptcha_tags %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是指一个索引路径,您似乎没有,但可能正在重定向到它?你能在问题中发表你对控制器代码的评论吗?当然,我添加了控制器代码。我做了你指定的更改,但我仍然得到了未定义的方法“comments\u index\u path”,因为它与我在原始问题中抱怨的错误在同一行。你好@Dave,我看到了你修改的控制器代码。我猜您的应用程序模型命名约定是错误的。推荐的模型名称应该是Comment而不是Comments.Hi@Dave,您也可以使用以下命令重新生成注释支架-rails generate scaffold Comment text:string