Quando si invia un modulo per creare una nuova richiesta con un commento figlio (nell'app, le richieste possono avere più commenti), il commento non viene generato. Funziona quando si rimuovono le convalide di presenza. Quindi ha a che fare con l'ordine in cui le cose sono costruite e salvate. Come conservare le convalide e mantenere il codice pulito?ID associazione non viene impostato utilizzando accept_nested_attributes_for e decent_exposure
(Il seguente è un esempio in modo che non può essere esattamente runable)
modelli/inquiry.rb
class Inquiry < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
modelli/comment.rb
class Comment < ActiveRecord::Base
belongs_to :inquiry
belongs_to :user
validates_presence_of :user_id, :inquiry_id
controllori/inquiry_controller. rb
expose(:inquiries)
expose(:inquiry)
def new
inquiry.comments.build :user => current_user
end
def create
# inquiry.save => false
# inquiry.valid? => false
# inquiry.errors => {:"comments.inquiry_id"=>["can't be blank"]}
end
views/Richieste/new.html.haml
= simple_form_for inquiry do |f|
= f.simple_fields_for :comments do |c|
= c.hidden_field :user_id
= c.input :body, :label => 'Comment'
= f.button :submit
schema del database
create_table "inquiries", :force => true do |t|
t.string "state"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "comments", :force => true do |t|
t.integer "inquiry_id"
t.integer "user_id"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
Si prega di mostrare le migrazioni che costruiscono queste tabelle, o se non sono state costruite attraverso migrazioni si prega di mostrare database descrive. –
ho aggiunto un estratto dello schema.rb – linojon