6

Bir çok ilgili gönderi okuyorum ancak neden benim için çalışmadığını bulamıyorum. Hâlâ "korumalı öznitelikleri atayamıyorum: profiller" var ... Neyi yanlış yapıyorum?Korunan özniteliklere kütle atayamazsınız: profiller,

Kullanıcı modelim ve birebir ilişki içeren bir İlişkili Profil modelim var. İşte Kullanıcı modeli (basitleştirilmiş)

class User < ActiveRecord::Base 
    attr_accessible :email, :password, :password_confirmation, :profile_attributes, :profile_id 
    has_secure_password 

    has_one :profile 

    accepts_nested_attributes_for :profile 
end 

Profil modeli

class Profile < ActiveRecord::Base 
attr_accessible :bio, :dob, :firstname, :gender, :lastname, :user_id 

belongs_to :user 
end 

benim Kullanıcı kontrolörü

def new 
@user = User.new 
    respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @user } 
    end 
end 

def create 
@user = User.new(params[:user]) 
@user.build_profile 

respond_to do |format| 
    if @user.save 

    format.html { redirect_to @user, flash: {success: 'User was successfully created. Welcome !'} } 
    format.json { render json: @user, status: :created, location: @user } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
end 
end 

herhangi bir yardım olabilirse, hem Kullanıcı ve Profil iskele yapıldı.

Birlikte çok çalıştık ': profiles_attributes' yerine Kullanıcı attr_accessible, aynı sorun içinde 'profile_attributes' arasında ...

yerine '@ user.build_profile' arasında 'user.profiles.build @' de çalıştı Kullanıcı kontrolörü ... aynı sonuç ... bazı açıklamalarla

Herhangi bir yardım çok iyi olurdu ben

kullanmak

Edit basit bir formu (I raylar üzerinde bir çaylak yüzden beni affet değilim)

<%= simple_form_for(@user) do |f| %> <%= f.error_notification %> <%= f.simple_fields_for :profiles do |p| %> <div class="nested-form-inputs"> <%= p.input :lastname %> <%= p.input :firstname %> </div> <% end %> <div class="form-inputs"> <%= f.input :email %> <%= f.input :password %> <%= f.input :password_confirmation %> </div> <div class="form-actions"> <%= f.button :submit %> </div> <% end %> 

Alkış

cevap

5

Eğer alıntı hata mesajı can't mass-assign protected attributes: profiles söylüyor. Sana ben sunucuyu yeniden başlattıktan ve her şey mükemmel çalışıyor attr_accessible :profiles (veya muhtemelen :profile)

Sorun raylar sınıf önbelleği ile oldu

accepts_nested_attributes_for :order_items 
attr_accessible :order_item 
+0

bunu çözdü, sorun rayların önbelleği ile oldu, ben rails sunucusunu yeniden başlattım ve her şey mükemmel çalışıyor. – ajet

3

sahip bir uygulama var bir gerektiğine inanıyorum ..,