在rails 4中,我生成了这个模型:
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 :(得分:1)
您需要修改视图(html和json)以显示新属性。