Ich lese viele verwandte Beiträge, kann aber nicht finden, warum es nicht für mich funktioniert. Ich habe immer noch eine "kann nicht geschützt Attribute zuweisen: Profile" ... Was mache ich falsch?Kann keine geschützten Attribute zuweisen: Profile,
Ich habe ein Benutzermodell und ein verwandtes Profilmodell mit einer Eins-zu-eins-Beziehung. Hier wird das Modell Benutzer (vereinfacht)
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
das Profil Modell
class Profile < ActiveRecord::Base
attr_accessible :bio, :dob, :firstname, :gender, :lastname, :user_id
belongs_to :user
end
meine Benutzer Controller
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
Wenn es eine Hilfe sein kann, sowohl Benutzer und Profil wurden eingerüstet.
Ich versuchte auch mit ': profiles_attributes' anstelle von 'profile_attributes' in dem Benutzer attr_accessible, die gleiche Problem ...
auch versucht, '@ user.profiles.build' anstelle von '@ user.build_profile' im User-Controller ... gleiche Ergebnis ...
Jede Hilfe mit einer Erklärung wäre toll (ich bin ein noob auf Schienen so vergib mir)
EDIT die einfache Form verwende ich
<%= 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 %>
Prost
löste dies, Problem war mit dem Rails Class Cache, ich habe den Rails Server neu gestartet und alles funktioniert perfekt. – ajet