繁体   English   中英

如何设置Rails表单以提交给create方法?

[英]How do I set up my Rails form to submit to a create method?

我正在使用Rails 5,但对如何设置表单以使其提交给我的“创建”控制器方法感到困惑。 这是我设置的路线

  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>

我不确定要使表格正常工作还要做什么。 在我的控制器中,我同时具有“创建”和“新”方法。

编辑:这是控制器

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

我认为,您的应用程序comments_controller的新操作应该是这样的-

# 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 %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM