数据正在发布但不会进入数据库

时间:2012-04-29 10:44:50

标签: ruby-on-rails ruby ajax ruby-on-rails-3

我正在尝试将HTML从可疑的DIV保存到数据库中。数据发布正常,但在数据库插入时,它们是nil

这是我的日志:

Started POST "/pages" for 127.0.0.1 at 2012-04-29 18:39:01 +0800
Processing by PagesController#create as */*
  Parameters: {"authenticity_token"=>"yAkOgEXN1Xq6Aiu6thcODiji7rs0SYA07/83XkQ2sBM=", "title"=>"Untitled Page", "live"=>"1", "content"=>"%3Cdiv%20class%3D%22page%20last-page-active%22%20style%3D%22height%3A%20576px%3B%20%22%3E%0A%20%20%20%20%0A%20%20%3Cdiv%20class%3D%22page-element%20page-element-textbox%20ui-draggable%20ui-resizable%20ui-resizable-disabled%20ui-state-disabled%22%20style%3D%22left%3A%201256px%3B%20top%3A%2080px%3B%20width%3A%20272px%3B%20%22%20aria-disabled%3D%22true%22%3E%3Cdiv%20class%3D%22page-element-content%22%20contenteditable%3D%22true%22%3E%3Ch2%3EUntitled%20Page%3C/h2%3E%3C/div%3E%3Cdiv%20class%3D%22ui-resizable-handle%20ui-resizable-e%22%20style%3D%22display%3A%20none%3B%20%22%3E%3C/div%3E%3Cdiv%20class%3D%22ui-resizable-handle%20ui-resizable-w%22%20style%3D%22display%3A%20none%3B%20%22%3E%3C/div%3E%3C/div%3E%0A%3C/div%3E"}
  SQL (0.2ms)  BEGIN
  SQL (1.2ms)  INSERT INTO `pages` (`content`, `created_at`, `live`, `title`, `updated_at`) VALUES (?, ?, ?, ?, ?)  [["content", nil], ["created_at", Sun, 29 Apr 2012 10:39:01 UTC +00:00], ["live", nil], ["title", nil], ["updated_at", Sun, 29 Apr 2012 10:39:01 UTC +00:00]]
   (1.9ms)  COMMIT

这是我的控制者:

class PagesController < ApplicationController
  def index
    @pages = Page.all
    @page = Page.new

    respond_to do |format|
      format.html  # index.html.erb
      format.json  { render :json => @pages }
    end
  end

  def new
    @page = Page.new

    respond_to do |format|
      format.html  # new.html.erb
      format.json  { render :json => @page }
    end
  end

  def create
    @page = Page.new(params[:page])

    respond_to do |format|
      if @page.save
        format.html  { redirect_to(@page,
                      :notice => 'Page was successfully created.') }
        format.json  { render :json => @page,
                      :status => :created, :location => @page }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @page.errors,
                      :status => :unprocessable_entity }
      end
    end
  end

  def show
    @page = Page.find(params[:id])

    respond_to do |format|
      format.html  # show.html.erb
      format.json  { render :json => @page }
    end
  end
end

2 个答案:

答案 0 :(得分:1)

@page = Page.new(params[:page])

应该是

@page = Page.new(params)

您可以在控制台输出中看到它:

Parameters: {"authenticity_token"=>"...", "title"=>"Untitled Page", "live"=>"1", "content"=>"..."}

如果params[:page]是正确的,您会看到:

Parameters: {"authenticity_token"=>"...", "page"=>{"title"=>"Untitled Page", "live"=>"1", "content"=>"..."}}

答案 1 :(得分:1)

您的控制器期望params[:page]是包含用于创建页面的值的哈希。但是,您可以在顶级提交所有这些参数,如日志文件摘录所示。

您应该更改表单,以便参数显示在params[:page]中,例如标题输入应命名为page[title]。如果你使用form_for并且rails帮助(f.text_fieldf.check_box等),你可以免费获得。