Rails表单不写入数据库

时间:2012-12-03 20:08:08

标签: mysql ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我在将新添加的列中的信息写入我的mysql数据库时遇到问题。特别是' site_email'。当我在本地运行项目时,我可以使用我的新列内容写入数据库,它将显示在随后的显示页面上。

然而在开发服务器上我不能。开发服务器日志显示请求中发送的内容。

Started PUT "/sites/18" for x.x.139.147 at 2012-12-03 19:47:39 +0000
      Processing by SitesController#update as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"6QXzdALrXqXoQz+Q5SFXd+egNQGxOAdNIC8ZH9A9yqU=", "site"=>{"customer_id"=>"1", "site_name"=>"Demohello", "site_domain"=>"Demo", "site_email"=>"test@email.comhello", "google_analytics_code"=>"", "visible"=>"1", "mobile_visible"=>"1", "logo_cache"=>"", "background_logo_cache"=>"", "header_cache"=>"", "background_cache"=>"", "campaign_wrapbar_cache"=>""}, "commit"=>"Update Site", "id"=>"18"}
    Redirected to http://x.com/sites/18
    Completed 302 Found in 17ms

但是当它进入显示页面时,新的/更新电子邮件数据不存在。奇怪的是,如果我将任何数据添加到预先存在的列以及其他数据已成功发送到数据库但电子邮件不是。

索引显示和编辑页面都显示没有错误,如果我手动更改数据库中的信息,它将显示在显示页面上。因为发送请求数据并且页面没有产生错误,我甚至完全无法调试它。

来自/ var / www / sitename / current我跑

bundle exec rake db:migrate

使用SequelPro连接到dev数据库我可以看到该列看起来就在那里,的确如果我在site_email列中插入一个数据,数据就会显示在显示页面上。

DB

http://i.imgur.com/QwAfC.png

显示带有插入值的页面

http://i.imgur.com/Sn7ws.png

_form.html.erb

<%= form_for(@site) do |f| %>
  <% if @site.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@site.errors.count, "error") %> prohibited this site from being saved:</h2>

    <ul>
      <% @site.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :customer_id %><br />
    <%#= f.text_field :customer %>
    <%= f.collection_select(:customer_id, Customer.all , :id , :customer_name ) %>

  </div>
  <div class="field">
    <%= f.label :site_name %><br />
    <%= f.text_field :site_name %>
  </div>
  <div class="field">
    <%= f.label :site_domain %><br />
    <%= f.text_field :site_domain %>
  </div>
    <div class="field">
    <%= f.label :site_email %><br />
    <%= f.text_field :site_email %>
  </div>
  <div class="field">
    <%= f.label :google_analytics_code %><br />
    <%= f.text_field :google_analytics_code %>
  </div>
  <div class="field">
   <%= f.label :visible %><br />
   <%= f.check_box :visible %><br />
  </div>
  <div class="field">
    <%= f.label :mobile_visible %><br />
    <%= f.check_box :mobile_visible %><br />
  </div>
  <div class="field">
    <%= f.label :logo %>
    <%= f.file_field :logo %>
    <%= f.hidden_field :logo_cache %><br />
    <%= image_tag(@site.logo_url) if @site.logo? %>
  </div>
  <div class="field">
    <%= f.label :background_logo %>
    <%= f.file_field :background_logo %>
    <%= f.hidden_field :background_logo_cache %><br />
    <%= image_tag(@site.background_logo_url) if @site.background_logo? %>
  </div>
  <div class="field">
    <%= f.label :header %>
    <%= f.file_field :header %>
    <%= f.hidden_field :header_cache %><br />
    <%= image_tag(@site.header_url) if @site.header? %>
  </div>
  <div class="field">
    <%= f.label :background %>
    <%= f.file_field :background %>
    <%= f.hidden_field :background_cache %><br />
    <%= image_tag(@site.background_url) if @site.background? %>
  </div>
  <div class="field">
    <%= f.label :campaign_wrapbar %>
    <%= f.file_field :campaign_wrapbar %>
    <%= f.hidden_field :campaign_wrapbar_cache %><br />
    <%= image_tag(@site.campaign_wrapbar_url) if @site.campaign_wrapbar? %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

show.html.erb

<section id="otherpage">
  <div class="otherpagetitle"> <%= @site.site_name %></div>
  <div class="otherpagetext dark_border_24_no_pad">

    <p>
      <b>Site name:</b>
      <%= @site.site_name %>
    </p>

    <p>
      <b>Site customer:</b>
      <%= @site.customer %>
    </p>

    <p>
      <b>Site domain:</b>
      <%= @site.site_domain %>
    </p>

    <p>
      <b>Site email:</b>
      <%= @site.site_email %>
    </p>

    <p>
      <b>Google analytics code:</b>
      <%= @site.google_analytics_code %>
    </p>

    <div class="field">
      <p>
        <b>Visible: </b>
        <%= check_box_tag "Visible", "yes", @site.visible, disabled: true %>
      </p>
    </div>
    <div class="field">
      <p>
        <b>Mobile Visible: </b>
        <%= check_box_tag "Mobile Visible", "yes", @site.mobile_visible, disabled: true %>
      </p>
    </div>
    <div class="field">
      <p>
        <b>Logo: </b><br />
        <%= @site.logo? ? image_tag(@site.logo_url) : "No logo uploaded" %>
      </p>
    </div>
    <div class="field">
      <p>
        <b>Background Logo: </b><br />
        <%= @site.background_logo? ? image_tag(@site.background_logo_url) : "No background logo uploaded" %>
      </p>
    </div>
    <%= link_to 'Edit', edit_site_path(@site) %> |
    <%= link_to 'Back', sites_path %>
  </div>
</section>

edit.html.erb

<section id="otherpage">

      <div class="otherpagetitle">Editing Site</div>
      <div class="otherpagetext dark_border_24_no_pad">


        <%= render 'form' %>

        <%= link_to 'Show', @site %> |
        <%= link_to 'Back', sites_path %>
      </div>
    </section>

sites_controller.rb

class SitesController < ApplicationController
  load_and_authorize_resource


  # GET /sites
  # GET /sites.json
  def index
    @sites = Site.all
    @site = Site.first(conditions: { site_domain: request.subdomain }) || not_found

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

  # GET /sites/1
  # GET /sites/1.json
  def show
    @site = Site.find(params[:id])

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

  # GET /sites/new
  # GET /sites/new.json
  def new
    @site = Site.new

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

  # GET /sites/1/edit
  def edit
    @site = Site.find(params[:id])
  end

  # POST /sites
  # POST /sites.json
  def create
    @site = Site.new(params[:site])

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

  # PUT /sites/1
  # PUT /sites/1.json
  def update
    @site = Site.find(params[:id])

    respond_to do |format|
      if @site.update_attributes(params[:site])
        format.html { redirect_to @site, notice: 'Site was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sites/1
  # DELETE /sites/1.json
  def destroy
    @site = Site.find(params[:id])
    @site.destroy

    respond_to do |format|
      format.html { redirect_to sites_url }
      format.json { head :ok }
    end
  end

  def site_styles
    @site = Site.first(conditions: { site_domain: request.subdomain }) || not_found

    respond_to do |format|
      format.html
      format.json { render json: @site }
      format.css
    end
  end
end

Gemlock.file

GEM
  remote: http://rubygems.org/
  specs:
    actionmailer (3.1.5)
      actionpack (= 3.1.5)
      mail (~> 2.3.3)
    actionpack (3.1.5)
      activemodel (= 3.1.5)
      activesupport (= 3.1.5)
      builder (~> 3.0.0)
      erubis (~> 2.7.0)
      i18n (~> 0.6)
      rack (~> 1.3.6)
      rack-cache (~> 1.2)
      rack-mount (~> 0.8.2)
      rack-test (~> 0.6.1)
      sprockets (~> 2.0.4)
    activemodel (3.1.5)
      activesupport (= 3.1.5)
      builder (~> 3.0.0)
      i18n (~> 0.6)
    activerecord (3.1.5)
      activemodel (= 3.1.5)
      activesupport (= 3.1.5)
      arel (~> 2.2.3)
      tzinfo (~> 0.3.29)
    activeresource (3.1.5)
      activemodel (= 3.1.5)
      activesupport (= 3.1.5)
    activesupport (3.1.5)
      multi_json (>= 1.0, < 1.3)
    ansi (1.4.2)
    arel (2.2.3)
    bcrypt-ruby (3.0.1)
    best_in_place (1.0.6)
      jquery-rails
      rails (~> 3.1)
    builder (3.0.0)
    cancan (1.6.7)
    capistrano (2.11.2)
      highline
      net-scp (>= 1.0.0)
      net-sftp (>= 2.0.0)
      net-ssh (>= 2.0.14)
      net-ssh-gateway (>= 1.1.0)
    capistrano-ext (1.2.1)
      capistrano (>= 1.0.0)
    carrierwave (0.5.8)
      activesupport (~> 3.0)
    ckeditor (3.7.0.rc2)
      mime-types (~> 1.17.2)
      orm_adapter (~> 0.0.6)
    coffee-rails (3.1.1)
      coffee-script (>= 2.2.0)
      railties (~> 3.1.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.2.0)
    daemons (1.1.8)
    devise (1.5.3)
      bcrypt-ruby (~> 3.0)
      orm_adapter (~> 0.0.3)
      warden (~> 1.1)
    erubis (2.7.0)
    eventmachine (0.12.10)
    execjs (1.2.13)
      multi_json (~> 1.0)
    friendly_id (4.0.1)
    has_scope (0.5.1)
    highline (1.6.11)
    hike (1.2.1)
    i18n (0.6.0)
    inherited_resources (1.2.2)
      has_scope (~> 0.5.0)
      responders (~> 0.6.0)
    jquery-rails (1.0.19)
      railties (~> 3.0)
      thor (~> 0.14)
    json (1.7.3)
    mail (2.3.3)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.17.2)
    mini_magick (3.4)
      subexec (~> 0.2.1)
    minitest (2.6.1)
    multi_json (1.2.0)
    mysql2 (0.3.11)
    net-scp (1.0.4)
      net-ssh (>= 1.99.1)
    net-sftp (2.0.5)
      net-ssh (>= 2.0.9)
    net-ssh (2.3.0)
    net-ssh-gateway (1.1.0)
      net-ssh (>= 1.99.1)
    orm_adapter (0.0.6)
    polyglot (0.3.3)
    rack (1.3.6)
    rack-cache (1.2)
      rack (>= 0.4)
    rack-mount (0.8.3)
      rack (>= 1.0.0)
    rack-ssl (1.3.2)
      rack
    rack-test (0.6.1)
      rack (>= 1.0)
    rails (3.1.5)
      actionmailer (= 3.1.5)
      actionpack (= 3.1.5)
      activerecord (= 3.1.5)
      activeresource (= 3.1.5)
      activesupport (= 3.1.5)
      bundler (~> 1.0)
      railties (= 3.1.5)
    rails-i18n (0.3.0)
      i18n (~> 0.5)
    railties (3.1.5)
      actionpack (= 3.1.5)
      activesupport (= 3.1.5)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (~> 0.14.6)
    rake (0.9.2.2)
    rdoc (3.12)
      json (~> 1.4)
    responders (0.6.5)
    sass (3.1.15)
    sass-rails (3.1.4)
      actionpack (~> 3.1.0)
      railties (~> 3.1.0)
      sass (>= 3.1.4)
      sprockets (~> 2.0.0)
      tilt (~> 1.3.2)
    sprockets (2.0.4)
      hike (~> 1.2)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sqlite3 (1.3.5)
    subexec (0.2.1)
    thin (1.3.1)
      daemons (>= 1.0.9)
      eventmachine (>= 0.12.6)
      rack (>= 1.0.0)
    thor (0.14.6)
    tilt (1.3.3)
    treetop (1.4.10)
      polyglot
      polyglot (>= 0.3.1)
    truncate_html (0.5.4)
    turn (0.8.3)
      ansi
    tzinfo (0.3.33)
    uglifier (1.2.3)
      execjs (>= 0.3.0)
      multi_json (>= 1.0.2)
    warden (1.1.0)
      rack (>= 1.0)
    will_paginate (3.0.3)

PLATFORMS
  ruby

DEPENDENCIES
  best_in_place
  cancan
  capistrano-ext
  carrierwave
  ckeditor (= 3.7.0.rc2)
  coffee-rails (~> 3.1.1)
  devise (~> 1.5)
  execjs (~> 1.2.11)
  friendly_id
  inherited_resources
  jquery-rails
  mini_magick
  minitest
  mysql2 (~> 0.3.11)
  rails (~> 3.1.4)
  rails-i18n
  sass-rails (= 3.1.4)
  sqlite3
  thin
  truncate_html
  turn
  uglifier (>= 1.0.3)
  will_paginate (~> 3.0)

迁移

class AddSiteEmailColumnToSites < ActiveRecord::Migration
      def change
        add_column :sites, :site_email, :string
      end
    end

capistrano deploy

cap staging deploy
    triggering load callbacks
  * 2012-12-03 20:45:57 executing `staging'
    triggering start callbacks for `deploy'
  * 2012-12-03 20:45:57 executing `multistage:ensure'
Identity added: /Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)
  * 2012-12-03 20:45:57 executing `deploy'
  * 2012-12-03 20:45:57 executing `deploy:update'
 ** transaction: start
  * 2012-12-03 20:45:57 executing `deploy:update_code'
    executing locally: "git ls-remote ssh://gitolite@site_repo:2011/proto_mysitename_rails HEAD"
    command finished in 2997ms
  * executing "git clone -q ssh://gitolite@site_repo:2011/proto_mysitename_rails /var/www/sitename/releases/20121203204600 && cd /var/www/sitename/releases/20121203204600 && git checkout -q -b deploy ed9ca3a2f10b049ed17b952d90ec7b61e9e98822 && (echo ed9ca3a2f10b049ed17b952d90ec7b61e9e98822 > /var/www/sitename/releases/20121203204600/REVISION)"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 80618ms
  * 2012-12-03 20:47:21 executing `deploy:finalize_update'
    triggering before callbacks for `deploy:finalize_update'
  * 2012-12-03 20:47:21 executing `deploy:assets:symlink'
  * executing "rm -rf /var/www/sitename/releases/20121203204600/public/assets &&\\\n        mkdir -p /var/www/sitename/releases/20121203204600/public &&\\\n        mkdir -p /var/www/sitename/shared/assets &&\\\n        ln -s /var/www/sitename/shared/assets /var/www/sitename/releases/20121203204600/public/assets"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 118ms
  * executing "chmod -R -- g+w /var/www/sitename/releases/20121203204600 && rm -rf -- /var/www/sitename/releases/20121203204600/public/system && mkdir -p -- /var/www/sitename/releases/20121203204600/public/ && ln -s -- /var/www/sitename/shared/system /var/www/sitename/releases/20121203204600/public/system && rm -rf -- /var/www/sitename/releases/20121203204600/log && ln -s -- /var/www/sitename/shared/log /var/www/sitename/releases/20121203204600/log && rm -rf -- /var/www/sitename/releases/20121203204600/tmp/pids && mkdir -p -- /var/www/sitename/releases/20121203204600/tmp/ && ln -s -- /var/www/sitename/shared/pids /var/www/sitename/releases/20121203204600/tmp/pids"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 120ms
    triggering after callbacks for `deploy:update_code'
  * 2012-12-03 20:47:21 executing `deploy:assets:precompile'
  * executing "cd /var/www/sitename/releases/20121203204600 && rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
 ** [out :: x.x.x.x] /usr/local/bin/ruby /usr/local/bin/rake assets:precompile:nondigest RAILS_ENV=production RAILS_GROUPS=assets
    command finished in 101748ms
  * 2012-12-03 20:49:03 executing `deploy:create_symlink'
  * executing "rm -f /var/www/sitename/current && ln -s /var/www/sitename/releases/20121203204600 /var/www/sitename/current"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 111ms
 ** transaction: commit
  * 2012-12-03 20:49:03 executing `deploy:restart'
  * executing "rm /var/www/sitename/current/config/database.yml"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 65ms
  * executing "ln -s /var/www/sitename/shared/database.yml /var/www/sitename/current/config/database.yml"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 65ms
  * executing "ln -s /var/www/sitename/shared/session_store.rb /var/www/sitename/current/config/initializers/session_store.rb"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 69ms
  * executing "ln -s /var/www/sitename/shared/public/uploads /var/www/sitename/current/public/uploads"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 63ms
  * executing "ln -s /var/www/sitename/shared/public/ckeditor_assets /var/www/sitename/current/public/ckeditor_assets"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 62ms
  * executing "sudo -p 'sudo password: ' touch /var/www/sitename/current/tmp/restart.txt"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command

对此问题的任何帮助或建议都非常欢迎。

谢谢

更新

在不更改内容的情况下,值会立即写入数据库。 24个左右。我现在遇到与实时服务器相同的问题。我正在等待另外24小时,看看是否对问题进行了分类,或者我是否意外地以其他方式修复了问题。

以防其他人将来有这个问题,有没有人知道为什么它会以这种方式行事?

更新2

实时服务器现在也更新了值。似乎服务器可能处于高负载或数据库以某种方式播放。

参考

如果您尝试在评论中尝试所有建议并参考Thomas&#39;回答这个应该在99.9%的情况下解决问题。如果它在本地工作但不在服务器上,并且您已经尝试了所有这些步骤,那么它可能与我所遇到的问题相同,不幸的是我目前还没有真正的答案。

1 个答案:

答案 0 :(得分:2)

这可能是某种质量分配问题,如果除非已经进行了某些设置,否则属性会被静默删除。

尝试在您的网站模型中对:site_email的通话中添加attr_accessible

# app/models/site.rb
...
attr_accessible :name, :site_email # mass-assignment protection

您需要列出您希望能够通过控制器中的@site.update_attributes(params[:site]更新的所有属性,并使用Site.new(name: ..., site_email: ...)或所谓的质量分配进行实例化,或者Site.create(name: ..., site_email: ...)