ruby-on-rails - 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
解决方案
推荐阅读
- javascript - 定时器功能多长时间
- comsol - 周期性结构和非周期性元素
- jquery-ui - 如何使用 Apple 的 VoiceOver 屏幕阅读器导航 jQuery UI 菜单?
- java - 需要帮助在 Java 游戏中创建敌人
- c# - gRPC Asp Net Core 3.1 API 在 Kubernetes 上运行时不起作用
- python-3.x - 为熊猫列中的每个唯一值在另一列中查找最大值
- java - Java Spring Boot Query where cond on Jsonb column
- repository - 我想替换 colab 脚本中的行,需要一些帮助
- webpack - 如何使用 webpack 和开发服务器使用 TinyMCE 设置皮肤?
- excel - 如何复制和粘贴区域“n”次,其中 n = 计数行数?