首页 > 解决方案 > 博客应用程序在提交表单时未将属性保存到数据库

问题描述

我正在按照指南构建一个带有导轨的博客,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 能够创建带有正文和标题的帖子,并了解有关简单形式的更多信息。

提前致谢!

标签: ruby-on-railssimple-form-for

解决方案


您编写@post = Post.new时没有将参数传递给您的对象,因此当您保存对象时,您会保存一个空对象。

它应该是:

@post = Post.new(post_params)

或者直接

@post = Post.create(post_params)


推荐阅读