Witam, posiadam model User i Password. Chciałbym aby z formularza gdzie będą pola login i password_txt zrobił się wpis do modelu Password i User (wraz z id password). Poniżej kod:
Migracja User
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :login, null: false
t.references :password, index: true, foreign_key: true, null: false
t.timestamps null: false
end
end
end
Migracja Password
class CreatePasswords < ActiveRecord::Migration
def change
create_table :passwords do |t|
t.string :password_txt, null: false
t.references :user, index: true, foreign_key: true, null: false #jeżeli to niepotrzebne to bez tego
t.timestamps null: false
end
end
end
Model User
class User < ActiveRecord::Base
has_one :password
end
Model Password
class Password < ActiveRecord::Base
belongs_to :user
end
Widok formularza dodania usera wraz z hasłem
<%= form_for @user do |f| %>
<table>
<tr>
<th><%= f.label :login %></th><td><%= f.text_field :login %></td>
</tr>
<tr>
<%= f.fields_for :password do |p| %>
<th><%= p.label :password_txt %></th><td><%= p.text_field :password_txt %></td>
<% end %>
</tr>
<tr><td></td><td><%= f.submit %></td></tr>
</table>
<% end %>
A mój problem jest taki, że za nic w świecie nie udaje mi się utworzyć usera, abym miał w tabeli users login i id password, które password jest (powinno być) razem tworzone.
Czy w ogóle da się to tak zrobić jak wymyśliłem. Czy asocjacje są poprawne?
Ten testowy projekt jest też tu: https://ide.c9.io/mariuszeu/test-password
Z góry dzięki za pomoc.