내가 처음으로 레일 프로젝트에서 일하고 있어요, 나는 다음과 같은 모델의 관계가 있습니다레일 : belongs_to & accepts_nested_attributes_for, 다형성
class Profile < ActiveRecord::Base
belongs_to :identifiable, polymorphic: true
accepts_nested_attributes_for :students
class Student < ActiveRecord::Base
has_one :profile, as: :identifiable
attr_accessible :profile
관련 컨트롤러는을 :
class StudentsController < ApplicationController
def new
@student = Student.new
end
def create
@student = Student.new(params[:student])
if @student.save
redirect_to root_path
else
render 'new'
end
end
end
그리고
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(params[:profile])
@profile.save
end
end
내가하려는 일은 다음과 함께 새 Student
을 생성하는 것입니다. No association found for name 'students'. Has it been defined yet?
내가 잘못 뭐하는 거지 : 나는 양식을 제출하려고 할 때 다음과 같은 오류 메시지가납니다
<h1>Create a new Student Account</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@student) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.fields_for :profile, @profile do |builder| %>
<%= builder.label :name %>
<%= builder.text_field :name %>
<%= builder.label :email %>
<%= builder.text_field :email %>
<%= builder.label :password %>
<%= builder.password_field :password %>
<%= builder.label :password_confirmation, "Confirmation" %>
<%= builder.password_field :password_confirmation %>
<% end %>
</div>
</div>
<p><%= f.submit "Submit", class: "btn btn-large btn-primary" %></p>
<% end %>
: students\new.html.erb
에 양식을 보내고? 미리 감사드립니다.
그걸 고쳤습니다. 감사! – ucarion
또한 내 문제를 해결했습니다 –
학생 대신 프로필 양식을 사용하는 방법이 있습니까? – Vla