首页 > 解决方案 > Formtastic 表单未提交 - rails 6

问题描述

我正在尝试在 _form.html.erb 文件中创建一个非常简单的提交表单。但是,表单未提交。如果我尝试创建一个新帖子,在我点击提交按钮后,页面只是刷新并且没有任何内容保存到数据库中。我正在使用 Rails 6.0.2.1。

这是_form.html.erb下的表格

<form>
  <%= semantic_form_for @post do |f| %>
    <div class="form-row">
      <%= f.inputs do %>
        <div class="form-group col-md-12">
          <%= f.input :title, :label => 'Title', :required => true, :wrapper_html => { :class => "important", :size => 10 } %>
        </div>
        <div class="form-group col-md-12">
          <%= f.input :body, :label => 'Post', :required => true, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 500  } %>
        </div>
        <div class="form-group col-md-12">
          <%= f.input :section, :label => 'Tag', :as => :select, :collection => ["Finance", "Arts/Music", "Sports", "Mood", "Technology", "Health", "News", "Politics", "Literature", "Food", "Life", "Pet", "Shopping"] %>
        </div>
        <div class="form-group col-md-12">
          <%= f.input :location, :as => :country, :label => 'Where are you posting' %>
        </div>
        <div class="form-group col-md-12">
          <%= f.input :username %>
        </div>
      <% end %>
    </div>
    <%= f.actions do %>
      <%= f.action :submit, label: "Submit" %>
    <% end %>
  <% end %>
</form>

post_controller.rb 的一部分

class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    "puts creating post"
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def post_params
    params.require(:post).permit(:title, :body, :section, :location, :username)
  end
end

模型/post.rb

class Post < ApplicationRecord
    attr_accessor :title, :body, :section, :location, :username
end

标签: ruby-on-railsformtastic

解决方案


推荐阅读