Form associations: Having trouble with controller saving information to 2 models simultaneously
For my project I am trying to build a dashboard whereby an Agent can view submissions posted by a user and add a Status & Notes to each submission in order to log their own personal activity i.e they would not be changing the actual record, just leaving private notes against it. In order to do this I have created a join table with both Agent id and Submission id as well as Status and Notes columns.
I have managed to create an index view that shows submissions data with 2 form fields at the end of each line from my join table which are called Status and Notes... the problem is when I update these fields they do not get saved to my jointable.
Form on index view
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
<% end %>
Model associations in rb files
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
end
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true #has submission_id
foreign key in table
belongs_to :agent, :optional => true #has agent_id foreign key in
table
end
Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
def index
@submissions = Submission.where(:user_id => current_user.id)
end
def show
end
def new
@submission = Submission.new
end
def edit
end
# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params.merge(user_id: current_user.id))
respond_to do |format|
if @submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(@submission).deliver_now
NewSubmissionMailer.matching_agents_email(@submission).deliver_now
format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: @submission }
else
format.html { render :new }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if @submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
@submission = Submission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end
Not sure what im missing here :/
UPDATE BASED OFF @TOM ANSWER
New controller params:
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end
end
New Submission Model rb:
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
accepts_nested_attributes_for :agent_activities
end
Index.html.erb
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
ruby-on-rails ruby forms html-table associations
add a comment |
For my project I am trying to build a dashboard whereby an Agent can view submissions posted by a user and add a Status & Notes to each submission in order to log their own personal activity i.e they would not be changing the actual record, just leaving private notes against it. In order to do this I have created a join table with both Agent id and Submission id as well as Status and Notes columns.
I have managed to create an index view that shows submissions data with 2 form fields at the end of each line from my join table which are called Status and Notes... the problem is when I update these fields they do not get saved to my jointable.
Form on index view
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
<% end %>
Model associations in rb files
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
end
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true #has submission_id
foreign key in table
belongs_to :agent, :optional => true #has agent_id foreign key in
table
end
Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
def index
@submissions = Submission.where(:user_id => current_user.id)
end
def show
end
def new
@submission = Submission.new
end
def edit
end
# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params.merge(user_id: current_user.id))
respond_to do |format|
if @submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(@submission).deliver_now
NewSubmissionMailer.matching_agents_email(@submission).deliver_now
format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: @submission }
else
format.html { render :new }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if @submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
@submission = Submission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end
Not sure what im missing here :/
UPDATE BASED OFF @TOM ANSWER
New controller params:
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end
end
New Submission Model rb:
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
accepts_nested_attributes_for :agent_activities
end
Index.html.erb
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
ruby-on-rails ruby forms html-table associations
Can you show the rest of your controller? Specifically your strong parameters? Did you add the newstatus
andnotes
as keys in your permitted parameters?
– Tom
Jan 2 at 16:40
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52
add a comment |
For my project I am trying to build a dashboard whereby an Agent can view submissions posted by a user and add a Status & Notes to each submission in order to log their own personal activity i.e they would not be changing the actual record, just leaving private notes against it. In order to do this I have created a join table with both Agent id and Submission id as well as Status and Notes columns.
I have managed to create an index view that shows submissions data with 2 form fields at the end of each line from my join table which are called Status and Notes... the problem is when I update these fields they do not get saved to my jointable.
Form on index view
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
<% end %>
Model associations in rb files
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
end
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true #has submission_id
foreign key in table
belongs_to :agent, :optional => true #has agent_id foreign key in
table
end
Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
def index
@submissions = Submission.where(:user_id => current_user.id)
end
def show
end
def new
@submission = Submission.new
end
def edit
end
# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params.merge(user_id: current_user.id))
respond_to do |format|
if @submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(@submission).deliver_now
NewSubmissionMailer.matching_agents_email(@submission).deliver_now
format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: @submission }
else
format.html { render :new }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if @submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
@submission = Submission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end
Not sure what im missing here :/
UPDATE BASED OFF @TOM ANSWER
New controller params:
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end
end
New Submission Model rb:
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
accepts_nested_attributes_for :agent_activities
end
Index.html.erb
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
ruby-on-rails ruby forms html-table associations
For my project I am trying to build a dashboard whereby an Agent can view submissions posted by a user and add a Status & Notes to each submission in order to log their own personal activity i.e they would not be changing the actual record, just leaving private notes against it. In order to do this I have created a join table with both Agent id and Submission id as well as Status and Notes columns.
I have managed to create an index view that shows submissions data with 2 form fields at the end of each line from my join table which are called Status and Notes... the problem is when I update these fields they do not get saved to my jointable.
Form on index view
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
<% end %>
Model associations in rb files
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
end
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true #has submission_id
foreign key in table
belongs_to :agent, :optional => true #has agent_id foreign key in
table
end
Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
def index
@submissions = Submission.where(:user_id => current_user.id)
end
def show
end
def new
@submission = Submission.new
end
def edit
end
# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params.merge(user_id: current_user.id))
respond_to do |format|
if @submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(@submission).deliver_now
NewSubmissionMailer.matching_agents_email(@submission).deliver_now
format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: @submission }
else
format.html { render :new }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if @submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
@submission = Submission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end
Not sure what im missing here :/
UPDATE BASED OFF @TOM ANSWER
New controller params:
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end
end
New Submission Model rb:
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
accepts_nested_attributes_for :agent_activities
end
Index.html.erb
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
ruby-on-rails ruby forms html-table associations
ruby-on-rails ruby forms html-table associations
edited Jan 2 at 18:15


Brian Tompsett - 汤莱恩
4,2421339102
4,2421339102
asked Jan 2 at 16:29


Werner AdewoleWerner Adewole
267
267
Can you show the rest of your controller? Specifically your strong parameters? Did you add the newstatus
andnotes
as keys in your permitted parameters?
– Tom
Jan 2 at 16:40
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52
add a comment |
Can you show the rest of your controller? Specifically your strong parameters? Did you add the newstatus
andnotes
as keys in your permitted parameters?
– Tom
Jan 2 at 16:40
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52
Can you show the rest of your controller? Specifically your strong parameters? Did you add the new
status
and notes
as keys in your permitted parameters?– Tom
Jan 2 at 16:40
Can you show the rest of your controller? Specifically your strong parameters? Did you add the new
status
and notes
as keys in your permitted parameters?– Tom
Jan 2 at 16:40
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52
add a comment |
1 Answer
1
active
oldest
votes
On your
Submission
model add:accepts_nested_attributes_for :agent_activities
(accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.Once that is added Rails will be supplying a key in params
agent_activities_attributes
in your strong params we can add:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]
. The:_destroy
key is only needed if you plan on havingallow_destroy: true
on the nested attribute call.
One side note: Capitalized names (Status
, Notes
, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR tryform_with(model: @submission..
using the instance variable instead of a symbol reference.
– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009843%2fform-associations-having-trouble-with-controller-saving-information-to-2-models%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
On your
Submission
model add:accepts_nested_attributes_for :agent_activities
(accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.Once that is added Rails will be supplying a key in params
agent_activities_attributes
in your strong params we can add:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]
. The:_destroy
key is only needed if you plan on havingallow_destroy: true
on the nested attribute call.
One side note: Capitalized names (Status
, Notes
, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR tryform_with(model: @submission..
using the instance variable instead of a symbol reference.
– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
add a comment |
On your
Submission
model add:accepts_nested_attributes_for :agent_activities
(accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.Once that is added Rails will be supplying a key in params
agent_activities_attributes
in your strong params we can add:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]
. The:_destroy
key is only needed if you plan on havingallow_destroy: true
on the nested attribute call.
One side note: Capitalized names (Status
, Notes
, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR tryform_with(model: @submission..
using the instance variable instead of a symbol reference.
– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
add a comment |
On your
Submission
model add:accepts_nested_attributes_for :agent_activities
(accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.Once that is added Rails will be supplying a key in params
agent_activities_attributes
in your strong params we can add:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]
. The:_destroy
key is only needed if you plan on havingallow_destroy: true
on the nested attribute call.
One side note: Capitalized names (Status
, Notes
, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
On your
Submission
model add:accepts_nested_attributes_for :agent_activities
(accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.Once that is added Rails will be supplying a key in params
agent_activities_attributes
in your strong params we can add:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]
. The:_destroy
key is only needed if you plan on havingallow_destroy: true
on the nested attribute call.
One side note: Capitalized names (Status
, Notes
, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
answered Jan 2 at 17:01


TomTom
1,194718
1,194718
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR tryform_with(model: @submission..
using the instance variable instead of a symbol reference.
– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
add a comment |
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR tryform_with(model: @submission..
using the instance variable instead of a symbol reference.
– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Thanks @Tom very helpful although my form fields on my index view seem to now have disappeared?
– Werner Adewole
Jan 2 at 17:10
Try
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR try form_with(model: @submission..
using the instance variable instead of a symbol reference.– Tom
Jan 2 at 17:13
Try
<% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
This will supply a record object in the event there are no pre existing agent_activities. OR try form_with(model: @submission..
using the instance variable instead of a symbol reference.– Tom
Jan 2 at 17:13
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
So the second option seems to work, however, my button has changed from updated to save and it is now asking me to save to a completely different model (called estate agents) which isn't working because I have not added to the required params for it.
– Werner Adewole
Jan 2 at 17:21
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
estate agents?? could that be a typo somewhere?
– Tom
Jan 2 at 17:25
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
It seems to be coming from me using the instance variable as opposed to the symbol reference in the form
– Werner Adewole
Jan 2 at 17:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009843%2fform-associations-having-trouble-with-controller-saving-information-to-2-models%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show the rest of your controller? Specifically your strong parameters? Did you add the new
status
andnotes
as keys in your permitted parameters?– Tom
Jan 2 at 16:40
Updated now, no I have not included status and notes included params because I figured that I would not be updating any of the columns in the submissions model?
– Werner Adewole
Jan 2 at 16:52