在導軌4,我生成該模型:爲什麼我的Rails新列沒有顯示在客戶端的模型響應中?
social_account.rb
class SocialAccount < ActiveRecord::Base
belongs_to :company
end
然後我意識到我需要的活性柱。所以我創造了這個遷移:
20140129134020_add_active_to_social_accounts.rb
class AddSelectedToSocialAccounts < ActiveRecord::Migration
def change
add_column :social_accounts, :active, :boolean
end
end
我然後跑rake db:migrate
和它的工作如預期。另外,我通過運行rails console
確認新的字段在我的模型中。
在控制檯我跑了這一點:
SocialAccount.find(10).to_json
並返回此如我所料:
SocialAccount Load (5.5ms) SELECT "social_accounts".* FROM "social_accounts" WHERE "social_accounts"."id" = ? LIMIT 1 [["id", 10]]
=> "{\"id\":10,\"vendor\":\"twitter\",\"created_at\":\"2014-01-30T02:05:56.106Z\",\"updated_at\":\"2014-01-30T02:05:56.106Z\",\"company_id\":1,\"active\":true}"
主要的一點是它包含active
列。
但現在在我的瀏覽器我試試這個: $.getJSON('/api/social_accounts/10.json').success(function(data){console.log(data)})
但它返回沒有active
柱:
created_at: "2014-01-30T02:05:56.106Z"
id: 10
updated_at: "2014-01-30T02:05:56.106Z"
vendor: "twitter"
而且,我的控制器看起來是這樣的:
social_accounts_controller.rb
class SocialAccountsController < ApplicationController
before_action :set_social_account, only: [:show, :update, :destroy]
...
# GET /social_accounts/1
# GET /social_accounts/1.json
def show
end
...
private
def set_user
@user = User.find(session[:user_id])
end
def set_social_accounts_for_user
set_user
@social_accounts = @user.company.social_accounts
end
# Use callbacks to share common setup or constraints between actions.
def set_social_account
set_social_accounts_for_user
@social_account = @social_accounts.find_by(id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def social_account_params
params.permit(:vendor, :active)
end
end
爲什麼是active
列不顯示?我需要清除一些緩存嗎?它沒有顯示,因爲它是一個布爾字段?
您能否提供您的show動作的'respond_to'區塊和@zwippie要求的視圖代碼? – Patru