2014-01-30 10 views
0

在導軌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列不顯示?我需要清除一些緩存嗎?它沒有顯示,因爲它是一個布爾字段?

+0

您能否提供您的show動作的'respond_to'區塊和@zwippie要求的視圖代碼? – Patru

回答

1

你需要修改你的視圖(html和json)來顯示新的屬性。

+0

我是小白。我不知道有一個像json視圖這樣的概念。我只是假定它會自動序列化模型並將其發送到響應中而不會顯示。我只需要添加列到我的JSON視圖。 – justspamjustin

相關問題