I have Test
model and User
model. Test
model is having many users
. Test controller is as below.
class TestController
def create
Test.create(testparams)
end
private
def testparams
params.require(:test).permit(:test_name,user_attributes:[:user_name])
end
end
In the above code new Test would be created. I want to create new users for a existing test.How to do that??
You should be able to apply the same principles. Below is a basic framework which you will have to alter depending on your requirements.
test model
accepts_nested_attributes_for :users, allow_destroy: true
tests_controller
def edit
@test = Test.find(params["id"]
@test.users.build
end
def update
@test = Test.find(params["id"]
@test.update(testparams)
end
test view
<%= form_for @test do |f| %>
<%= f.text_field :test_name %>
<%= f.fields_for :users do |uf| %>
<%= uf.text_field :user_name %>
<% end %>
<% end %>