2012-12-03 60 views
0

我遇到一個問題,從新添加的列向我的mysql數據庫寫入信息。特別是'site_email'。當我在本地運行項目時,可以用向我的新列內容寫入數據庫,並顯示在隨後的顯示頁面上。Rails表單不能寫入數據庫

但是在dev服務器上我不行。開發服務器日誌顯示請求中發送的內容。

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"=>"[email protected]", "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 /網絡/網站名稱/電流我跑

bundle exec rake db:migrate 

使用SequelPro我可以看到,列看起來是有連接到開發數據庫,​​而事實上,如果我在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的部署

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://[email protected]_repo:2011/proto_mysitename_rails HEAD" 
    command finished in 2997ms 
    * executing "git clone -q ssh://[email protected]_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 

在這個問題上的任何幫助或建議是歡迎。

謝謝

UPDATE

不改變事物的價值寫信給數據庫。 24日左右。我現在與現場服務器有同樣的問題。我再等24小時,看看是否這樣排序問題,或者我不小心以其他方式解決問題。

萬一別人有這個問題在未來,沒有任何人有一個想法,爲什麼它的行爲可能以這種方式?

更新2

Live服務器現在更新值也是如此。這似乎是服務器可能負載很重或數據庫以某種方式播放。

參考

僅供參考,如果你試試在評論中所有建議,也看托馬斯的回答這樣就可以解決問題的所有情況下,99.9%。如果它在本地工作,但不在服務器上,並且您已經嘗試了所有這些步驟,那麼這可能與我遇到的問題相同,但不幸的是,目前我沒有真正的答案。

+0

請出示您的控制器代碼.. – Lichtamberg

+0

你遷移數據庫?添加了 – samullen

+0

。感謝您的光臨。 @samullen我已經添加了rake命令我跑了 –

回答

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: ...)實例。

+0

謝謝你的建議 –

+0

最終項目啓動數據庫到數據庫中(約24小時後)進入,他們現在更新立竿見影。任何想法爲什麼在功能啓動之前有一段延遲? –

+0

對不起,我不知道爲什麼會有24小時的延遲。它通常應該立即更新,並且只有在數據庫處於中等負載時才需要幾秒鐘。可能是你的具體設置的怪癖。 –