Khi tôi đăng biểu mẫu để tạo một yêu cầu mới với nhận xét con (trong ứng dụng, yêu cầu có thể có nhiều nhận xét), nhận xét không được xây dựng. Nó hoạt động khi loại bỏ các xác nhận hiện diện. Vì vậy, nó đã làm với thứ tự mà trong đó mọi thứ được xây dựng và lưu lại. Làm thế nào để bảo tồn các hợp lệ và giữ mã sạch?id liên kết không được đặt bằng cách sử dụng accept_nested_attributes_for và decent_exposure
(Sau đây là một ví dụ như vậy nó có thể không được chính xác runable)
mô hình/inquiry.rb
class Inquiry < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
mô hình/comment.rb
class Comment < ActiveRecord::Base
belongs_to :inquiry
belongs_to :user
validates_presence_of :user_id, :inquiry_id
controllers/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/thắc mắc/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
CSDL
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
Vui lòng hiển thị các di chuyển xây dựng các bảng này hoặc nếu không được xây dựng thông qua di chuyển, vui lòng hiển thị cơ sở dữ liệu mô tả. –
tôi cũng đã thêm một trích đoạn của schema.rb – linojon