博客应用程序在提交表单时未将属性保存到数据库
Posted
技术标签:
【中文标题】博客应用程序在提交表单时未将属性保存到数据库【英文标题】:Blog App not saving attributes to database upon form submission 【发布时间】:2019-12-23 15:16:51 【问题描述】:我正在按照指南使用 Rails 构建博客,简单的 Post
模型带有标题和正文。
我正在使用简单的表单并在提交表单时创建一个新帖子,该帖子保存 created_at 和 updated_at 值,但不保存表单中提交的实际内容。
我尝试删除简单表单的代码并使用 Rails 原生 form_for。这确实将所有值保存到数据库中。我是简单形式的新手,不确定我是否正确使用它。
这是控制台记录:
Started POST "/posts" for ::1 at 2019-08-17 13:51:01 -0500
Processing by PostsController#create as html
Parameters: "utf8"=>"✓", "authenticity_token"=>"qY8kYZxVIMBL8lzHYuQ4qOu6nXsTGLWCRhLPJ2eiAU8EyzR61fZppAFBYmgcm3rx02FYAHcCgFBVlUyDTLtDGA==", "post"=>"title"=>"Simple Form Test", "body"=>"<p>Test Test Test</p>\r\n", "commit"=>"Create Post"
(0.0ms) begin transaction
SQL (3.3ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2019-08-17 18:51:01.325736"], ["updated_at", "2019- 08-17 18:51:01.325736"]]
(7.7ms) commit transaction
Redirected to http://localhost:3000/posts/3
Completed 302 Found in 28ms (ActiveRecord: 11.1ms)
这是表格:
<%= simple_form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#pluralize(@post.errors.count, "error") prohibited this post from being saved:" %>
</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.input :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :body, :as => :ckeditor, input_html: :ckeditor => :toolbar => 'FULL', class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit %>
</div>
<% end %>
这里是控制器:
class PostsController < ApplicationController
before_action :find_post, only: [:edit, :update, :show, :delete]
# Index action to render all posts
def index
@posts = Post.all
end
# New action for creating post
def new
@post = Post.new
end
# Create action saves the post into database
def create
@post = Post.new
if @post.save(post_params)
flash[:notice] = "Successfully created post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
# Edit action retrives the post and renders the edit page
def edit
end
# Update action updates the post with the new information
def update
if @post.update_attributes(post_params)
flash[:notice] = "Successfully updated post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error updating post!"
render :edit
end
end
# The show action renders the individual post after retrieving the the id
def show
end
# The destroy action removes the post permanently from the database
def destroy
if @post.destroy
flash[:notice] = "Successfully deleted post!"
redirect_to posts_path
else
flash[:alert] = "Error updating post!"
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
end
Hopin
g 能够创建带有正文和标题的帖子,并了解有关简单表单的更多信息。
提前致谢!
【问题讨论】:
我们要猜测你控制器中的代码吗? 对不起,我知道我忘记了什么!现已添加 【参考方案1】:你写了@post = Post.new
没有将参数传递给你的对象,所以当你保存你的对象时,你保存了一个空对象。
应该是:
@post = Post.new(post_params)
或者直接
@post = Post.create(post_params)
【讨论】:
以上是关于博客应用程序在提交表单时未将属性保存到数据库的主要内容,如果未能解决你的问题,请参考以下文章