2014-07-07 99 views
0

我有一個稱爲isp的模型,has_many isp賬戶。 isp賬戶屬於'isp'。嵌套窗體不顯示在Rails中

對isp_account有一個驗證,意味着如果沒有isp_id,它就不能被添加,所以我的推理是創建一個嵌套窗體。我創建了這樣的嵌套表格

= simple_form_for @isp, :html => { :multipart => true } do |f| 
    = f.input :title 
    = f.simple_fields_for :isp_accounts do |tag| 
    = tag.input :title, label: "Tag Name" 

但是,嵌套屬性不被顯示。沒有錯誤等。這是爲什麼?我以最好的方式接近這個嗎?這是唯一的方法嗎?

這裏的代碼

ISP模式

class Isp < ActiveRecord::Base 
    has_many :isp_accounts, dependent: :destroy 
    has_many :deployments, through: :servers 
    has_many :servers, through: :isp_accounts 
    validates :title, presence: true 

    accepts_nested_attributes_for :isp_accounts 


end 

ISP帳戶MODEL

class IspAccount < ActiveRecord::Base 

    belongs_to :isp 
    has_many :deployments, through: :servers 
    has_many :servers, dependent: :destroy 

    validates :title, presence: true 
    validate :check_associates 

    private 

    def check_associates 
    associated_object_exists Isp, :isp_id 
    end 
end 

ISP賬戶控制器

.... 
    def new 
     @isp_account = IspAccount.new 
    end 

    def update 
    @isp_account.update_attributes(isp_accounts_path) 
    if @isp_account.save 
     record_saved 
     return redirect_to(isp_accounts_path) 
    else 
     check_for_errors 
     return render('/isp_accounts/edit') 
    end 
    end 

    private 

    def get_isp_accounts 
     @isp_account = IspAccount.all 
    end 

    def get_isp_account 
     @isp_account = IspAccount.find(params_isp_accounts) 
    end 
    def params_isp_accounts 
    params.require(:isp_account).permit! 
    end 
end 

.... 
    def new 
    @isp = Isp.new 
    end 

    def update 
    @isp.update_attributes(params_isp) 
    if @isp.save 
     record_saved 
     return redirect_to(isps_path) 
    else 
     check_for_errors 
     return render('new') 
    end 
    end 

    private 

    def params_isp 
    params.require(:isp).permit(:title, isp_accounts_attributes: [:id, :title]) 
    end 

    def get_isp 
    @isp = Isp.where(id: params[:id]).first 
    unless @isp 
     record_not_found 
     return redirect_to(isps_path) 
    end 
    end 

    def get_isps 
    @isp = Isp.all.order(:title) 
    end 
end 

SCHEMA

create_table "isp_accounts", force: true do |t| 
    t.string "title" 
    t.integer "isp_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "isps", force: true do |t| 
    t.string "title" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

回答

0

好的我明白了。我在控制器中遺漏了該屬性的新位。真的很基本。

def new 
    @isp = Isp.new 
    @isp.isp_accounts.new 
end