Last active
December 20, 2015 01:29
-
-
Save monteirobrena/6049891 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a.rb | |
class A < ActiveRecord::Base | |
has_one :b | |
accepts_nested_attributes_for :b | |
end | |
# b.rb | |
class B < ActiveRecord::Base | |
belongs_to :a | |
attr_accessible :name | |
end | |
# a_controller.rb | |
class AController < ApplicationController | |
def new | |
@a = A.new | |
@a.build_b | |
end | |
def create | |
@a = A.new(params[:a]) | |
@a.save | |
render :index | |
end | |
end | |
# new.html.erb | |
<%= form_for(@a) do |f| %> | |
<%= f.fields_for :b do |b| %> | |
<%= b.label t('b.name') %> | |
<%= b.text_field :name %> | |
<% end %> | |
<% end %> | |
# b_migration.rb | |
class CreateBs < ActiveRecord::Migration | |
def change | |
create_table :bs do |t| | |
t.references :a | |
t.timestamps | |
end | |
add_index :bs, :a_id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@monteirobrena Você diz que mesmo assim não está salvando? Acho estranho.. Só debugando então, se souber usar a gem Pry coloque um
binding.pry
após o#save
. Caso não saiba, sugiro estudar, vai te ajudar bastante: http://howtocode.com.br/screencasts/pry, mas dá pra fazer com um logger.info, veja se o model está válido adicionando umlogger.info @a.errors.instpect
e aí olhe nos logs e veja qual erro é apontado.